AuthForm.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. using FastReport.Forms;
  2. using FastReport.Utils;
  3. using System;
  4. using System.Drawing;
  5. using System.Drawing.Drawing2D;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using System.Windows.Forms;
  9. using FastReport.Cloud.FastReport;
  10. namespace FastReport.Auth
  11. {
  12. public partial class AuthForm : BaseDialogForm
  13. {
  14. #region Private Fields
  15. private string expiresFormat;
  16. private static int selectedIndex = 0;
  17. private static string customAPIkey = "";
  18. private static string customServerName = "";
  19. private CancellationTokenSource tokenSource;
  20. #endregion Private Fields
  21. public static bool isFirstLoad = true;
  22. public AuthForm()
  23. {
  24. InitializeComponent();
  25. Localize();
  26. AuthService.Instance.Refresh();
  27. UpdateInfo();
  28. UIUtils.CheckRTL(this);
  29. UpdateDpiDependencies();
  30. tokenSource = new CancellationTokenSource();
  31. }
  32. public override void UpdateDpiDependencies()
  33. {
  34. base.UpdateDpiDependencies();
  35. lbCaption.Font = lbCaptionServer.Font = this.LogicalToDevice(new Font("Calibri Light", 19.5f), true);
  36. if (RightToLeft == RightToLeft.Yes)
  37. {
  38. lbCaption.Left = pbAvatar.Right - lbCaption.Width;
  39. lbCaptionServer.Left = pbAvatar.Right - lbCaptionServer.Width;
  40. }
  41. }
  42. public override void Localize()
  43. {
  44. base.Localize();
  45. MyRes res = new MyRes("Forms,AccountWindow");
  46. Text = res.Get("");
  47. lbCaption.Text = res.Get("Title");
  48. btnSignIn.Text = res.Get("SignIn");
  49. lbNoAccount.Text = res.Get("NoAccount");
  50. lbCreateOne.Text = res.Get("Create");
  51. lbMessage.Text = res.Get("Intro") + res.Get("Testing");
  52. expiresFormat = res.Get("Expires");
  53. lbManage.Text = res.Get("Profile");
  54. lbSignOut.Text = res.Get("SignOut");
  55. lbText.Text = res.Get("Message");
  56. lbUpdate.Text = res.Get("SessionExpired");
  57. MyRes res2 = new MyRes("Forms,ServerWindow");
  58. pgServer.Text = res2.Get("");
  59. label1Server.Text = res2.Get("Server");
  60. label2Server.Text = res2.Get("ApiKeyword");
  61. lbCaptionServer.Text = res2.Get("Caption");
  62. comboBoxServerServer.Items[0] = res2.Get("comboBoxServer");
  63. comboBoxServerServer.Items[1] = res2.Get("comboBoxCustom");
  64. }
  65. protected override void OnFormClosed(FormClosedEventArgs e)
  66. {
  67. base.OnFormClosed(e);
  68. if (DialogResult == DialogResult.OK)
  69. {
  70. if (!string.IsNullOrEmpty(AuthService.Instance.User.ApiKey) && !string.IsNullOrEmpty(FRCloudOptions.Instance.BackendHost))
  71. FRCloudOptions.Instance.SaveCustomServerInfo();
  72. else
  73. FRCloudOptions.Instance.DeleteCustomServerInfo();
  74. }
  75. tokenSource.Cancel();
  76. tokenSource.Dispose();
  77. }
  78. private async void btnSignIn_Click(object sender, EventArgs e)
  79. {
  80. await Task.Run(async () =>
  81. {
  82. await AuthService.Instance.SignIn(tokenSource.Token);
  83. }, tokenSource.Token);
  84. UpdateInfo();
  85. }
  86. private void lbCreateOne_Click(object sender, EventArgs e)
  87. {
  88. ProcessHelper.StartProcess("https://id.fast-report.com/");
  89. }
  90. private void lbManage_Click(object sender, EventArgs e)
  91. {
  92. ProcessHelper.StartProcess("https://id.fast-report.com/");
  93. }
  94. private async void lbSignOut_Click(object sender, EventArgs e)
  95. {
  96. await Task.Run(async () =>
  97. {
  98. await AuthService.Instance.SignOut(tokenSource.Token);
  99. }, tokenSource.Token);
  100. UpdateInfo();
  101. }
  102. private async void lbUpdate_Click(object sender, EventArgs e)
  103. {
  104. await Task.Run(async () =>
  105. {
  106. if (!AuthService.Instance.Refresh())
  107. await AuthService.Instance.SignIn(tokenSource.Token);
  108. }, tokenSource.Token);
  109. UpdateInfo();
  110. }
  111. //server
  112. private void comboBoxServerServer_SelectedValueChanged(object sender, EventArgs e)
  113. {
  114. var comboBox = sender as ComboBox;
  115. var value = comboBox.SelectedItem as string;
  116. if (value == Res.Get("Forms,ServerWindow,comboBoxServer")) // pre-written server
  117. {
  118. textBoxCustomHostServer.Text = Res.Get("Forms,ServerWindow,DefaultServerHost");
  119. textBoxCustomHostServer.Enabled = false;
  120. customServerName = "";
  121. }
  122. else // Custom
  123. {
  124. textBoxCustomHostServer.Enabled = true;
  125. textBoxCustomHostServer.Text = "";
  126. }
  127. if (isFirstLoad)
  128. {
  129. textBoxApiKeyServer.Text = customAPIkey;
  130. }
  131. else
  132. {
  133. textBoxApiKeyServer.Text = String.Empty;
  134. }
  135. }
  136. private void textBoxCustomHostServer_TextChanged(object sender, EventArgs e)
  137. {
  138. var textBox = sender as TextBox;
  139. if (!textBox.Enabled)
  140. return;
  141. var requestHost = textBox.Text;
  142. if (string.IsNullOrEmpty(requestHost))
  143. {
  144. }
  145. else
  146. {
  147. AuthService.Instance.User.ApiKey = AuthService.Instance.User.ApiKey == String.Empty
  148. ? String.Empty
  149. : AuthService.Instance.User.ApiKey;
  150. FRCloudOptions.Instance.BackendHost = requestHost;
  151. customServerName = requestHost;
  152. }
  153. }
  154. private void textBoxApiKeyServer_TextChanged(object sender, EventArgs e)
  155. {
  156. var textBox = sender as TextBox;
  157. var text = textBox.Text;
  158. if (text != null)
  159. {
  160. var apiKey = text.Trim();
  161. AuthService.Instance.User.ApiKey = apiKey;
  162. customAPIkey = apiKey;
  163. }
  164. }
  165. private Image RoundCorners(Image fromImage, float cornerRadius, Color backgroundColor)
  166. {
  167. cornerRadius *= 2;
  168. Bitmap result = new Bitmap(fromImage.Width, fromImage.Height);
  169. using (Graphics g = Graphics.FromImage(result))
  170. {
  171. g.Clear(backgroundColor);
  172. g.SmoothingMode = SmoothingMode.AntiAlias;
  173. using (Brush brush = new TextureBrush(fromImage))
  174. {
  175. using (GraphicsPath gp = new GraphicsPath())
  176. {
  177. gp.AddArc(0, 0, cornerRadius, cornerRadius, 180, 90);
  178. gp.AddArc(0 + result.Width - cornerRadius - 1, 0, cornerRadius, cornerRadius, 270, 90);
  179. gp.AddArc(0 + result.Width - cornerRadius - 1, 0 + result.Height - cornerRadius - 1, cornerRadius, cornerRadius, 0, 90);
  180. gp.AddArc(0, 0 + result.Height - cornerRadius, cornerRadius - 1, cornerRadius, 90, 90);
  181. g.FillPath(brush, gp);
  182. return result;
  183. }
  184. }
  185. }
  186. }
  187. private void UpdateInfo()
  188. {
  189. AuthService auth = AuthService.Instance;
  190. if (isFirstLoad)
  191. {
  192. XmlItem xi = Config.Root.FindItem("Auth").FindItem("ConnectionInfo");
  193. string serverFromConfig = xi.GetProp("Server");
  194. string apiKeyFromConfig = FRCloudOptions.Instance.GetDecodedString(xi.GetProp("ApiKey"));
  195. if (serverFromConfig != string.Empty && serverFromConfig != Res.Get("Forms,ServerWindow,DefaultServerHost") && apiKeyFromConfig != string.Empty)
  196. {
  197. comboBoxServerServer.SelectedIndex = 1;
  198. FRCloudOptions.Instance.BackendHost = serverFromConfig;
  199. AuthService.Instance.User.ApiKey = apiKeyFromConfig;
  200. customAPIkey = apiKeyFromConfig;
  201. customServerName = serverFromConfig;
  202. textBoxApiKeyServer.Text = apiKeyFromConfig;
  203. textBoxCustomHostServer.Text = serverFromConfig;
  204. }
  205. else
  206. {
  207. customAPIkey = apiKeyFromConfig;
  208. comboBoxServerServer.SelectedIndex = 0;
  209. textBoxApiKeyServer.Text = apiKeyFromConfig;
  210. }
  211. isFirstLoad = false;
  212. }
  213. else
  214. {
  215. comboBoxServerServer.SelectedIndex = selectedIndex;
  216. if (selectedIndex == 1)
  217. {
  218. textBoxCustomHostServer.Text = customServerName;
  219. }
  220. textBoxApiKeyServer.Text = customAPIkey;
  221. }
  222. if (auth.User.IsAuthenticated)
  223. {
  224. BeforeSignIn(false);
  225. AfterSignIn(true);
  226. lbUsername.Text = auth.User.DisplayName;
  227. lbEmail.Text = auth.User.DisplayEmail;
  228. pbAvatar.Image = RoundCorners(auth.User.DisplayAvatar, 15, Color.Transparent);
  229. if (auth.CanRefresh)
  230. {
  231. if (auth.User.ExpiresIn < DateTime.Now)
  232. {
  233. lbTime.Visible = false;
  234. lbUpdate.Visible = true;
  235. }
  236. else
  237. {
  238. lbUpdate.Visible = false;
  239. lbTime.Visible = true;
  240. lbTime.Text = String.Format(expiresFormat, auth.User.ExpiresIn.ToShortTimeString());
  241. }
  242. }
  243. else
  244. {
  245. // do not overlap with lbUpdate which is visible too
  246. lbTime.Visible = false;
  247. }
  248. }
  249. else
  250. {
  251. BeforeSignIn(true);
  252. AfterSignIn(false);
  253. }
  254. }
  255. private void BeforeSignIn(bool visible)
  256. {
  257. pgFrId.SuspendLayout();
  258. lbMessage.Visible = visible;
  259. btnSignIn.Visible = visible;
  260. lbCreateOne.Visible = visible;
  261. lbNoAccount.Visible = visible;
  262. pgFrId.PerformLayout();
  263. }
  264. private void AfterSignIn(bool visible)
  265. {
  266. pgFrId.SuspendLayout();
  267. lbUpdate.Visible = visible;
  268. lbTime.Visible = visible;
  269. pbAvatar.Visible = visible;
  270. lbText.Visible = visible;
  271. lbSignOut.Visible = visible;
  272. lbManage.Visible = visible;
  273. lbUsername.Visible = visible;
  274. lbEmail.Visible = visible;
  275. pgFrId.ResumeLayout(true);
  276. }
  277. private void comboBoxServerServer_SelectedIndexChanged(object sender, EventArgs e)
  278. {
  279. selectedIndex = comboBoxServerServer.SelectedIndex;
  280. }
  281. }
  282. }