123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- using FastReport.Forms;
- using FastReport.Utils;
- using System;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using FastReport.Cloud.FastReport;
- namespace FastReport.Auth
- {
- public partial class AuthForm : BaseDialogForm
- {
- #region Private Fields
- private string expiresFormat;
- private static int selectedIndex = 0;
- private static string customAPIkey = "";
- private static string customServerName = "";
- private CancellationTokenSource tokenSource;
- #endregion Private Fields
- public static bool isFirstLoad = true;
- public AuthForm()
- {
- InitializeComponent();
- Localize();
- AuthService.Instance.Refresh();
- UpdateInfo();
- UIUtils.CheckRTL(this);
- UpdateDpiDependencies();
- tokenSource = new CancellationTokenSource();
- }
- public override void UpdateDpiDependencies()
- {
- base.UpdateDpiDependencies();
- lbCaption.Font = lbCaptionServer.Font = this.LogicalToDevice(new Font("Calibri Light", 19.5f), true);
- if (RightToLeft == RightToLeft.Yes)
- {
- lbCaption.Left = pbAvatar.Right - lbCaption.Width;
- lbCaptionServer.Left = pbAvatar.Right - lbCaptionServer.Width;
- }
- }
- public override void Localize()
- {
- base.Localize();
- MyRes res = new MyRes("Forms,AccountWindow");
- Text = res.Get("");
- lbCaption.Text = res.Get("Title");
- btnSignIn.Text = res.Get("SignIn");
- lbNoAccount.Text = res.Get("NoAccount");
- lbCreateOne.Text = res.Get("Create");
- lbMessage.Text = res.Get("Intro") + res.Get("Testing");
- expiresFormat = res.Get("Expires");
- lbManage.Text = res.Get("Profile");
- lbSignOut.Text = res.Get("SignOut");
- lbText.Text = res.Get("Message");
- lbUpdate.Text = res.Get("SessionExpired");
- MyRes res2 = new MyRes("Forms,ServerWindow");
- pgServer.Text = res2.Get("");
- label1Server.Text = res2.Get("Server");
- label2Server.Text = res2.Get("ApiKeyword");
- lbCaptionServer.Text = res2.Get("Caption");
- comboBoxServerServer.Items[0] = res2.Get("comboBoxServer");
- comboBoxServerServer.Items[1] = res2.Get("comboBoxCustom");
- }
- protected override void OnFormClosed(FormClosedEventArgs e)
- {
- base.OnFormClosed(e);
- if (DialogResult == DialogResult.OK)
- {
- if (!string.IsNullOrEmpty(AuthService.Instance.User.ApiKey) && !string.IsNullOrEmpty(FRCloudOptions.Instance.BackendHost))
- FRCloudOptions.Instance.SaveCustomServerInfo();
- else
- FRCloudOptions.Instance.DeleteCustomServerInfo();
- }
- tokenSource.Cancel();
- tokenSource.Dispose();
- }
- private async void btnSignIn_Click(object sender, EventArgs e)
- {
- await Task.Run(async () =>
- {
- await AuthService.Instance.SignIn(tokenSource.Token);
- }, tokenSource.Token);
- UpdateInfo();
- }
- private void lbCreateOne_Click(object sender, EventArgs e)
- {
- ProcessHelper.StartProcess("https://id.fast-report.com/");
- }
- private void lbManage_Click(object sender, EventArgs e)
- {
- ProcessHelper.StartProcess("https://id.fast-report.com/");
- }
- private async void lbSignOut_Click(object sender, EventArgs e)
- {
- await Task.Run(async () =>
- {
- await AuthService.Instance.SignOut(tokenSource.Token);
- }, tokenSource.Token);
- UpdateInfo();
- }
- private async void lbUpdate_Click(object sender, EventArgs e)
- {
- await Task.Run(async () =>
- {
- if (!AuthService.Instance.Refresh())
- await AuthService.Instance.SignIn(tokenSource.Token);
- }, tokenSource.Token);
- UpdateInfo();
- }
- //server
- private void comboBoxServerServer_SelectedValueChanged(object sender, EventArgs e)
- {
- var comboBox = sender as ComboBox;
- var value = comboBox.SelectedItem as string;
- if (value == Res.Get("Forms,ServerWindow,comboBoxServer")) // pre-written server
- {
- textBoxCustomHostServer.Text = Res.Get("Forms,ServerWindow,DefaultServerHost");
- textBoxCustomHostServer.Enabled = false;
- customServerName = "";
- }
- else // Custom
- {
- textBoxCustomHostServer.Enabled = true;
- textBoxCustomHostServer.Text = "";
- }
- if (isFirstLoad)
- {
- textBoxApiKeyServer.Text = customAPIkey;
- }
- else
- {
- textBoxApiKeyServer.Text = String.Empty;
- }
- }
- private void textBoxCustomHostServer_TextChanged(object sender, EventArgs e)
- {
- var textBox = sender as TextBox;
- if (!textBox.Enabled)
- return;
- var requestHost = textBox.Text;
- if (string.IsNullOrEmpty(requestHost))
- {
- }
- else
- {
- AuthService.Instance.User.ApiKey = AuthService.Instance.User.ApiKey == String.Empty
- ? String.Empty
- : AuthService.Instance.User.ApiKey;
- FRCloudOptions.Instance.BackendHost = requestHost;
- customServerName = requestHost;
- }
- }
- private void textBoxApiKeyServer_TextChanged(object sender, EventArgs e)
- {
- var textBox = sender as TextBox;
- var text = textBox.Text;
- if (text != null)
- {
- var apiKey = text.Trim();
- AuthService.Instance.User.ApiKey = apiKey;
- customAPIkey = apiKey;
- }
- }
- private Image RoundCorners(Image fromImage, float cornerRadius, Color backgroundColor)
- {
- cornerRadius *= 2;
- Bitmap result = new Bitmap(fromImage.Width, fromImage.Height);
- using (Graphics g = Graphics.FromImage(result))
- {
- g.Clear(backgroundColor);
- g.SmoothingMode = SmoothingMode.AntiAlias;
- using (Brush brush = new TextureBrush(fromImage))
- {
- using (GraphicsPath gp = new GraphicsPath())
- {
- gp.AddArc(0, 0, cornerRadius, cornerRadius, 180, 90);
- gp.AddArc(0 + result.Width - cornerRadius - 1, 0, cornerRadius, cornerRadius, 270, 90);
- gp.AddArc(0 + result.Width - cornerRadius - 1, 0 + result.Height - cornerRadius - 1, cornerRadius, cornerRadius, 0, 90);
- gp.AddArc(0, 0 + result.Height - cornerRadius, cornerRadius - 1, cornerRadius, 90, 90);
- g.FillPath(brush, gp);
- return result;
- }
- }
- }
- }
- private void UpdateInfo()
- {
- AuthService auth = AuthService.Instance;
- if (isFirstLoad)
- {
- XmlItem xi = Config.Root.FindItem("Auth").FindItem("ConnectionInfo");
- string serverFromConfig = xi.GetProp("Server");
- string apiKeyFromConfig = FRCloudOptions.Instance.GetDecodedString(xi.GetProp("ApiKey"));
- if (serverFromConfig != string.Empty && serverFromConfig != Res.Get("Forms,ServerWindow,DefaultServerHost") && apiKeyFromConfig != string.Empty)
- {
- comboBoxServerServer.SelectedIndex = 1;
- FRCloudOptions.Instance.BackendHost = serverFromConfig;
- AuthService.Instance.User.ApiKey = apiKeyFromConfig;
- customAPIkey = apiKeyFromConfig;
- customServerName = serverFromConfig;
- textBoxApiKeyServer.Text = apiKeyFromConfig;
- textBoxCustomHostServer.Text = serverFromConfig;
- }
- else
- {
- customAPIkey = apiKeyFromConfig;
- comboBoxServerServer.SelectedIndex = 0;
- textBoxApiKeyServer.Text = apiKeyFromConfig;
- }
- isFirstLoad = false;
- }
- else
- {
- comboBoxServerServer.SelectedIndex = selectedIndex;
- if (selectedIndex == 1)
- {
- textBoxCustomHostServer.Text = customServerName;
- }
- textBoxApiKeyServer.Text = customAPIkey;
- }
- if (auth.User.IsAuthenticated)
- {
- BeforeSignIn(false);
- AfterSignIn(true);
- lbUsername.Text = auth.User.DisplayName;
- lbEmail.Text = auth.User.DisplayEmail;
- pbAvatar.Image = RoundCorners(auth.User.DisplayAvatar, 15, Color.Transparent);
- if (auth.CanRefresh)
- {
- if (auth.User.ExpiresIn < DateTime.Now)
- {
- lbTime.Visible = false;
- lbUpdate.Visible = true;
- }
- else
- {
- lbUpdate.Visible = false;
- lbTime.Visible = true;
- lbTime.Text = String.Format(expiresFormat, auth.User.ExpiresIn.ToShortTimeString());
- }
- }
- else
- {
- // do not overlap with lbUpdate which is visible too
- lbTime.Visible = false;
- }
- }
- else
- {
- BeforeSignIn(true);
- AfterSignIn(false);
- }
- }
- private void BeforeSignIn(bool visible)
- {
- pgFrId.SuspendLayout();
- lbMessage.Visible = visible;
- btnSignIn.Visible = visible;
- lbCreateOne.Visible = visible;
- lbNoAccount.Visible = visible;
- pgFrId.PerformLayout();
- }
- private void AfterSignIn(bool visible)
- {
- pgFrId.SuspendLayout();
- lbUpdate.Visible = visible;
- lbTime.Visible = visible;
- pbAvatar.Visible = visible;
- lbText.Visible = visible;
- lbSignOut.Visible = visible;
- lbManage.Visible = visible;
- lbUsername.Visible = visible;
- lbEmail.Visible = visible;
- pgFrId.ResumeLayout(true);
- }
- private void comboBoxServerServer_SelectedIndexChanged(object sender, EventArgs e)
- {
- selectedIndex = comboBoxServerServer.SelectedIndex;
- }
- }
- }
|