Esempio n. 1
0
        public async Task <IActionResult> SubmitUpdateAccount(GoblinIdentityUpdateIdentityModel model, CancellationToken cancellationToken = default)
        {
            if (!ModelState.IsValid)
            {
                ViewBag.WarningMessage = Messages.InvalidData;

                return(View("Account", model));
            }

            try
            {
                await GoblinIdentityHelper.UpdateIdentityAsync(LoggedInUser <GoblinIdentityUserModel> .Current.Data.Id, model, cancellationToken).ConfigureAwait(true);

                ViewBag.SuccessMessage = "Account Updated Successfully.";

                if (!string.IsNullOrWhiteSpace(model.NewPassword))
                {
                    return(View("~/Views/Auth/Login.cshtml", new LoginModel
                    {
                        Continue = Url.AbsoluteAction("Account", "Portal")
                    }));
                }
            }
            catch (GoblinException e)
            {
                ViewBag.ErrorMessage = e.ErrorModel.Message;
            }
            catch (Exception e)
            {
                ViewBag.ErrorMessage = e.Message;
            }

            return(View("Account", model));
        }
Esempio n. 2
0
        public static async Task Main(string[] args)
        {
            await Goblin.Core.Web.Setup.ProgramHelper.Main(args, webHostBuilder =>
            {
                webHostBuilder.UseStartup <Startup>();
            }, scope =>
            {
                // Register Role and Permission

                var roleModel = new GoblinIdentityUpsertRoleModel
                {
                    Name        = "Admin",
                    Permissions = new List <string>
                    {
                        "Member Manager",
                        "Blog Manager",
                        "Work Manager",
                        "Tech Manager"
                    }
                };

                var upsertRoleTask = GoblinIdentityHelper.UpsertRoleAsync(roleModel);

                upsertRoleTask.Wait();

                var upsertRoleResult = upsertRoleTask.Result;
            }
                                                           );
        }
Esempio n. 3
0
        public async Task <IActionResult> SubmitForgotPassword(ForgotPasswordModel model,
                                                               CancellationToken cancellationToken = default)
        {
            if (!ModelState.IsValid)
            {
                ViewBag.WarningMessage = Messages.InvalidData;

                return(View("ForgotPassword", model));
            }

            try
            {
                var requestResetPasswordModel = new GoblinIdentityRequestResetPasswordModel
                {
                    Email = model.Email
                };

                var resetPasswordToken =
                    await GoblinIdentityHelper.RequestResetPasswordAsync(requestResetPasswordModel, cancellationToken);

                var resetPasswordMessage = $"Your reset password code is {resetPasswordToken.SetPasswordToken}.";

                if (resetPasswordToken.SetPasswordTokenExpireTime.HasValue)
                {
                    resetPasswordMessage +=
                        $"<br />Code will expire at {resetPasswordToken.SetPasswordTokenExpireTime.Value.ToString("f")}";
                }

                var newEmailModel = new GoblinNotificationNewEmailModel
                {
                    ToEmails = new List <string>
                    {
                        model.Email
                    },
                    Subject  = $"{SystemSetting.Current.ApplicationName} | Reset Password Code",
                    HtmlBody = resetPasswordMessage
                };

                await GoblinNotificationHelper.SendAsync(newEmailModel, cancellationToken);

                return(View("ResetPassword", new ResetPasswordModel
                {
                    Email = model.Email
                }));
            }
            catch (GoblinException e)
            {
                ViewBag.ErrorMessage = e.ErrorModel.Message;

                return(View("ForgotPassword", model));
            }
            catch (Exception e)
            {
                ViewBag.ErrorMessage = e.Message;

                return(View("ForgotPassword", model));
            }
        }
Esempio n. 4
0
        public async Task <IActionResult> SubmitLogin([FromForm] LoginModel model,
                                                      CancellationToken cancellationToken = default)
        {
            if (LoggedInUser <GoblinIdentityUserModel> .Current?.Data != null)
            {
                if (!string.IsNullOrWhiteSpace(model.Continue))
                {
                    return(Redirect(model.Continue));
                }

                return(RedirectToAction("Index", "Home"));
            }

            if (!ModelState.IsValid)
            {
                ViewBag.WarningMessage = Messages.InvalidData;

                return(View("Login", model));
            }

            if (string.IsNullOrWhiteSpace(model.Continue))
            {
                model.Continue = Url.AbsoluteAction("Index", "Home");
            }

            ViewBag.ContinueUrl = model.Continue;

            try
            {
                var generateAccessTokenModel = new GoblinIdentityGenerateAccessTokenModel
                {
                    UserName = model.UserName,
                    Password = model.Password
                };

                var accessToken =
                    await GoblinIdentityHelper.GenerateAccessTokenAsync(generateAccessTokenModel, cancellationToken);

                accessToken = accessToken?.Trim('"');

                CookieHelper.SetShare(HttpContext, GoblinCookieKeys.AccessToken, accessToken);

                return(View("LoggedIn"));
            }
            catch (GoblinException e)
            {
                ViewBag.ErrorMessage = e.ErrorModel.Message;

                return(View("Login", model));
            }
            catch (Exception e)
            {
                ViewBag.ErrorMessage = e.Message;

                return(View("Login", model));
            }
        }
Esempio n. 5
0
        public async Task <IActionResult> VerifyEmail(CancellationToken cancellationToken = default)
        {
            var updateIdentityModel = new GoblinIdentityUpdateIdentityModel
            {
                NewUserName = LoggedInUser <GoblinIdentityUserModel> .Current.Data.UserName,
                NewEmail    = LoggedInUser <GoblinIdentityUserModel> .Current.Data.Email
            };

            if (LoggedInUser <GoblinIdentityUserModel> .Current.Data.EmailConfirmedTime.HasValue)
            {
                ViewBag.ErrorMessage = "Your email already verified!";

                return(View("Account", updateIdentityModel));
            }

            try
            {
                var emailConfirmationModel = await GoblinIdentityHelper.RequestConfirmEmailAsync(LoggedInUser <GoblinIdentityUserModel> .Current.Data.Id, cancellationToken).ConfigureAwait(true);

                // Send Email

                var confirmEmailMessage = $"Your verify email code is {emailConfirmationModel.EmailConfirmToken}.";

                if (emailConfirmationModel.EmailConfirmTokenExpireTime.HasValue)
                {
                    confirmEmailMessage += $"<br />Code will expire at {emailConfirmationModel.EmailConfirmTokenExpireTime.Value.ToString("f")}";
                }

                var newEmailModel = new GoblinNotificationNewEmailModel
                {
                    ToEmails = new List <string>
                    {
                        LoggedInUser <GoblinIdentityUserModel> .Current.Data.Email
                    },
                    Subject  = $"{SystemSetting.Current.ApplicationName} | Verify Your Email",
                    HtmlBody = confirmEmailMessage
                };

                await GoblinNotificationHelper.SendAsync(newEmailModel, cancellationToken);

                ViewBag.WarningMessage = "Please check your email inbox to get the Verify Code.";

                return(View());
            }
            catch (GoblinException e)
            {
                ViewBag.ErrorMessage = e.ErrorModel.Message;
            }
            catch (Exception e)
            {
                ViewBag.ErrorMessage = e.Message;
            }

            return(View("Account", updateIdentityModel));
        }
Esempio n. 6
0
        public async Task <IActionResult> SubmitResetPassword(ResetPasswordModel model, CancellationToken cancellationToken = default)
        {
            if (!ModelState.IsValid)
            {
                ViewBag.WarningMessage = Messages.InvalidData;

                return(View("ResetPassword", model));
            }

            try
            {
                var resetPasswordModel = new GoblinIdentityResetPasswordModel
                {
                    Email            = model.Email,
                    SetPasswordToken = model.SetPasswordToken,
                    NewPassword      = model.NewPassword
                };

                await GoblinIdentityHelper.ResetPasswordAsync(resetPasswordModel, cancellationToken);

                ViewBag.SuccessMessage = "Your account have updated password, now you can login with the new password.";

                return(View("Login"));
            }
            catch (GoblinException e)
            {
                ViewBag.ErrorMessage = e.ErrorModel.Message;

                return(View("ResetPassword", model));
            }
            catch (Exception e)
            {
                ViewBag.ErrorMessage = e.Message;

                return(View("ResetPassword", model));
            }
        }
Esempio n. 7
0
        public async Task <IActionResult> SubmitVerifyEmail(GoblinIdentityConfirmEmailModel model, CancellationToken cancellationToken = default)
        {
            if (!ModelState.IsValid)
            {
                ViewBag.WarningMessage = Messages.InvalidData;

                return(View("VerifyEmail", model));
            }

            try
            {
                model.LoggedInUserId = LoggedInUser <GoblinIdentityUserModel> .Current.Data.Id;

                await GoblinIdentityHelper.ConfirmEmailAsync(LoggedInUser <GoblinIdentityUserModel> .Current.Data.Id, model, cancellationToken).ConfigureAwait(true);

                ViewBag.SuccessMessage = "Your email is verified.";

                LoggedInUser <GoblinIdentityUserModel> .Current.Data.EmailConfirmedTime = GoblinDateTimeHelper.SystemTimeNow;
            }
            catch (GoblinException e)
            {
                ViewBag.ErrorMessage = e.ErrorModel.Message;
            }
            catch (Exception e)
            {
                ViewBag.ErrorMessage = e.Message;
            }

            var updateIdentityModel = new GoblinIdentityUpdateIdentityModel
            {
                NewUserName = LoggedInUser <GoblinIdentityUserModel> .Current.Data.UserName,
                NewEmail    = LoggedInUser <GoblinIdentityUserModel> .Current.Data.Email
            };

            return(View("Account", updateIdentityModel));
        }
Esempio n. 8
0
        public static void BindLoggedInUser(HttpContext httpContext)
        {
            var accessToken = CookieHelper.GetShare <string>(httpContext, GoblinCookieKeys.AccessToken);

            if (string.IsNullOrWhiteSpace(accessToken))
            {
                return;
            }

            try
            {
                var userModelTask = GoblinIdentityHelper.GetProfileByAccessTokenAsync(accessToken);

                userModelTask.Wait();

                var userModel = userModelTask.Result;

                LoggedInUser <GoblinIdentityUserModel> .Current = new LoggedInUser <GoblinIdentityUserModel>(userModel);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
Esempio n. 9
0
        public async Task <IActionResult> SubmitUpdateProfile(UpdateProfileModel model, CancellationToken cancellationToken = default)
        {
            if (!ModelState.IsValid)
            {
                ViewBag.WarningMessage = Messages.InvalidData;

                return(View("Profile", model));
            }

            try
            {
                var goblinIdentityUploadProfileUserModel = model.MapTo <GoblinIdentityUpdateProfileModel>();

                // Upload Avatar File if have

                if (model.AvatarFile != null)
                {
                    await using var memoryStream = new MemoryStream();

                    await model.AvatarFile.CopyToAsync(memoryStream, cancellationToken);

                    var fileBytes = memoryStream.ToArray();

                    var base64 = Convert.ToBase64String(fileBytes);

                    var fileName = model.AvatarFile.FileName ?? model.AvatarFile.Name ?? GoblinDateTimeHelper.SystemTimeNow.ToString("ddMMyyHHmmss");

                    var uploadResourceModel = new GoblinResourceUploadFileModel
                    {
                        LoggedInUserId        = LoggedInUser <GoblinIdentityUserModel> .Current.Data.Id,
                        Folder                = "avatars",
                        Name                  = $"user-{LoggedInUser<GoblinIdentityUserModel>.Current.Data.Id}-avatar-{fileName}",
                        ContentBase64         = base64,
                        ImageMaxHeightPx      = 800,
                        ImageMaxWidthPx       = 800,
                        IsEnableCompressImage = true
                    };

                    var fileModel = await GoblinResourceHelper.UploadAsync(uploadResourceModel, cancellationToken).ConfigureAwait(true);

                    goblinIdentityUploadProfileUserModel.AvatarUrl = fileModel.Slug;

                    // Update Model

                    LoggedInUser <GoblinIdentityUserModel> .Current.Data.AvatarUrl = model.AvatarUrl = goblinIdentityUploadProfileUserModel.AvatarUrl;

                    model.AvatarFile = null;
                }

                // Update Profile

                await GoblinIdentityHelper.UpdateProfileAsync(LoggedInUser <GoblinIdentityUserModel> .Current.Data.Id, goblinIdentityUploadProfileUserModel, cancellationToken).ConfigureAwait(true);

                ViewBag.SuccessMessage = "Profile Updated Successfully.";
            }
            catch (GoblinException e)
            {
                ViewBag.ErrorMessage = e.ErrorModel.Message;
            }
            catch (Exception e)
            {
                ViewBag.ErrorMessage = e.Message;
            }

            return(View("Profile", model));
        }