Пример #1
0
        // TODO: Assign user to the appropriate claims
        public async Task <IdentityResult> RegisterAsync(RegisterViewModel registerViewModel)
        {
            GolfioUser golfioUser = new GolfioUser
            {
                FirstName      = registerViewModel.FirstName,
                LastName       = registerViewModel.LastName,
                Email          = registerViewModel.Email,
                UserName       = registerViewModel.Email,
                OfficeLocation = Enum.GetName(typeof(OfficeLocation), Convert.ToInt32(registerViewModel.OfficeLocation)),
            };

            IdentityResult identityResult = await _userManager.CreateAsync(golfioUser, registerViewModel.Password);

            if (!identityResult.Succeeded)
            {
                return(identityResult);
            }

            var userCount = _userManager.Users.Count();

            if (userCount <= 1)
            {
                Claim newClaim = new Claim(ClaimType.Admin.ToString(), "true");
                identityResult = await _userManager.AddClaimAsync(golfioUser, newClaim);
            }
            else
            {
                Claim newClaim = new Claim(ClaimType.User.ToString(), "true");
                identityResult = await _userManager.AddClaimAsync(golfioUser, newClaim);
            }

            return(identityResult);
        }
Пример #2
0
        public async Task <bool> IsValidLoginAsync(GolfioUser golfioUser, string password)
        {
            bool isValidPassword = await _userManager.CheckPasswordAsync(golfioUser, password);

            bool isValidEmail = golfioUser.EmailConfirmed;

            return(isValidPassword && isValidEmail);
        }
Пример #3
0
        public async Task <IdentityResult> UpdateUserInfo(AdminEditViewModel adminEditVM)
        {
            GolfioUser golfioUser = await _userManager.FindByIdAsync(adminEditVM.UserId);

            golfioUser.FirstName          = adminEditVM.FirstName;
            golfioUser.LastName           = adminEditVM.LastName;
            golfioUser.Email              = adminEditVM.Email;
            golfioUser.UserName           = adminEditVM.Email;
            golfioUser.NormalizedUserName = adminEditVM.Email;
            golfioUser.NormalizedEmail    = adminEditVM.Email;

            return(await _userManager.UpdateAsync(golfioUser));
        }
Пример #4
0
        public void EmailToken(GolfioUser golfioUser, string tokenLink, EmailType emailType)
        {
            string EmailSubject;

            EmailSecret emailSecret = new EmailSecret();

            EmailSubject = "Please confirm your email";
            MailboxAddress from = new MailboxAddress("Golfio Admin", emailSecret.emailAddress);
            MailboxAddress to   = new MailboxAddress(golfioUser.FullName, golfioUser.Email);

            BodyBuilder bodyBuilder = new BodyBuilder();

            if (emailType == EmailType.EmailConfirmation)
            {
                bodyBuilder.HtmlBody =
                    $"<h1>Hello {golfioUser.FullName} </h1> \n\n" +
                    "<p>You've recently registered for Project Tracker</p> \n\n" +
                    "<p>Please click below to confirm your email address</p> \n\n" +
                    $"<a href='{tokenLink}'><button style='color:#fff; background-color:#007bff; border-color:#007bff;'>Confirm</button></a> \n\n" +
                    "<p>If the link doesn't work, you can copy and paste the below URL</p> \n\n" +
                    $"<p> {tokenLink} </p> \n\n\n" +
                    "<p>Thank you!</p>";
            }
            else
            {
                bodyBuilder.HtmlBody =
                    $"<h1>Hello {golfioUser.FullName} </h1> \n\n" +
                    "<p>You've recently requested for password reset</p> \n\n" +
                    "<p>Please click below to reset your password</p> \n\n" +
                    $"<a href='{tokenLink}'><button style='color:#fff; background-color:#007bff; border-color:#007bff;'>Confirm</button></a> \n\n" +
                    "<p>If the link doesn't work, you can copy and paste the below URL</p> \n\n" +
                    $"<p> {tokenLink} </p> \n\n\n" +
                    "<p>Thank you!</p>";
            }

            MimeMessage message = new MimeMessage();

            message.From.Add(from);
            message.To.Add(to);
            message.Subject = EmailSubject;
            message.Body    = bodyBuilder.ToMessageBody();

            using (SmtpClient client = new SmtpClient())
            {
                client.SslProtocols = SslProtocols.Tls;
                client.Connect(emailSecret.smtpServerAddress, emailSecret.port, emailSecret.useSSL);
                client.Authenticate(emailSecret.emailAddress, emailSecret.apiPassword);
                client.Send(message);
                client.Disconnect(true);
            }
        }
Пример #5
0
        public async Task <IdentityResult> DeleteAsync(string userId)
        {
            try
            {
                GolfioUser golfioUser = await _userManager.FindByIdAsync(userId);

                return(await _userManager.DeleteAsync(golfioUser));
            }
            catch (DbUpdateException e)
            {
                Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~");
                Console.WriteLine(e.InnerException);
                Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~");
                return(IdentityResult.Failed());
            }
        }
Пример #6
0
 public async Task <IdentityResult> ConfirmEmailTokenAsync(GolfioUser golfioUser, string token)
 {
     return(await _userManager.ConfirmEmailAsync(golfioUser, token));
 }
Пример #7
0
 public async Task <string> CreateEmailConfirmationToken(GolfioUser golfioUser)
 {
     return(await _userManager.GenerateEmailConfirmationTokenAsync(golfioUser));
 }
Пример #8
0
        public async Task <IdentityResult> ChangePasswordAsync(UserEditViewModel userEditVM)
        {
            GolfioUser golfioUser = await _userManager.FindByIdAsync(userEditVM.GolfioUser.Id);

            return(await _userManager.ChangePasswordAsync(golfioUser, userEditVM.OldPassword, userEditVM.NewPassword));
        }
Пример #9
0
        public async Task <IdentityResult> UpdateAccessPermission(AdminEditViewModel adminEditVM)
        {
            // Retrieve user Claims
            IdentityResult identityResult = new IdentityResult();
            GolfioUser     golfioUser     = await _userManager.FindByIdAsync(adminEditVM.UserId);

            List <Claim> userClaims = await GetUserClaimsAsync(golfioUser);

            // Does adminEditVM.ClaimInfos.ClaimType contain Admin?
            ClaimInfo adminClaimInfo = adminEditVM.ClaimInfos.FirstOrDefault(ci => ci.ClaimType == ClaimType.Admin.ToString() && ci.IsSelected == true);

            // If yes, check DB to see if there's Admin
            if (adminClaimInfo != null)
            {
                bool isClaimInDB = userClaims.Any(uc => uc.Type.ToString() == adminClaimInfo.ClaimType);

                // If no, add and remove all other claims
                if (isClaimInDB)
                {
                    List <Claim> nonAdminClaims = userClaims.Where(uc => uc.Type != ClaimType.Admin.ToString()).ToList();
                    await _userManager.RemoveClaimsAsync(golfioUser, nonAdminClaims);

                    return(IdentityResult.Success);
                }
                else
                {
                    await _userManager.RemoveClaimsAsync(golfioUser, userClaims);

                    Claim claim = new Claim(adminClaimInfo.ClaimType, "true");
                    return(await _userManager.AddClaimAsync(golfioUser, claim));
                }
            }

            // Separate ClaimValue True and False
            foreach (var claimInfo in adminEditVM.ClaimInfos)
            {
                if (claimInfo.IsSelected == true)
                {
                    bool isClaimInDB = userClaims.Any(uc => uc.Type.ToString() == claimInfo.ClaimType);

                    // If found, continue
                    if (isClaimInDB)
                    {
                        continue;
                    }
                    // If not found, create
                    else
                    {
                        Claim claim = new Claim(claimInfo.ClaimType, "true");
                        identityResult = await _userManager.AddClaimAsync(golfioUser, claim);
                    }
                }
                else
                {
                    Claim claimInDB = userClaims.FirstOrDefault(uc => uc.Type.ToString() == claimInfo.ClaimType);

                    // If found, delete
                    if (claimInDB != null)
                    {
                        identityResult = await _userManager.RemoveClaimAsync(golfioUser, claimInDB);

                        if (!identityResult.Succeeded)
                        {
                            return(identityResult);
                        }
                    }
                    // if not found, continue
                    else
                    {
                        continue;
                    }
                }

                if (!identityResult.Succeeded)
                {
                    return(identityResult);
                }
            }

            return(identityResult);
        }
Пример #10
0
 public async Task <List <Claim> > GetUserClaimsAsync(GolfioUser golfioUser)
 {
     return((await _userManager.GetClaimsAsync(golfioUser)).ToList());
 }
Пример #11
0
        public async Task <IdentityResult> ResetPasswordAsync(ResetPasswordViewModel resetPasswordVM)
        {
            GolfioUser golfioUser = await _db.GolfioUsers.FirstOrDefaultAsync(golfioUser => golfioUser.Email == resetPasswordVM.Email);

            return(await _userManager.ResetPasswordAsync(golfioUser, resetPasswordVM.Token, resetPasswordVM.Password));
        }
Пример #12
0
 public async Task <string> GeneratePasswordResetTokenAsync(GolfioUser golfioUser)
 {
     return(await _userManager.GeneratePasswordResetTokenAsync(golfioUser));
 }