public override bool IsUserInRole(string userName, string roleName)
 {
     if (string.IsNullOrEmpty(userName))
     {
         throw CreateArgumentNullOrEmptyException("userName");
     }
     if (string.IsNullOrEmpty(roleName))
     {
         throw CreateArgumentNullOrEmptyException("roleName");
     }
     using (BlogUnitOfWork context = new BlogUnitOfWork(new ModelContextInit()))
     {
         dynamic user = context.Users.FirstOrDefault(Usr => Usr.Username == userName);
         if (user == null)
         {
             return(false);
         }
         dynamic role = context.Roles.FirstOrDefault(Rl => Rl.RoleName == roleName);
         if (role == null)
         {
             return(false);
         }
         return(user.Roles.Contains(role));
     }
 }
 public override string GeneratePasswordResetToken(string userName, int tokenExpirationInMinutesFromNow)
 {
     if (string.IsNullOrEmpty(userName))
     {
         throw CreateArgumentNullOrEmptyException("userName");
     }
     using (BlogUnitOfWork context = new BlogUnitOfWork(new ModelContextInit()))
     {
         dynamic user = context.Users.FirstOrDefault(Usr => Usr.Username == userName);
         if (user == null)
         {
             throw new InvalidOperationException("El usuario no existe");
         }
         if (!user.IsConfirmed)
         {
             throw new InvalidOperationException("El usuario no existe");
         }
         string token = null;
         if (user.PasswordVerificationTokenExpirationDate > DateTime.UtcNow)
         {
             token = user.PasswordVerificationToken;
         }
         else
         {
             token = CodeFirstCrypto.GenerateToken();
         }
         user.PasswordVerificationToken = token;
         user.PasswordVerificationTokenExpirationDate = DateTime.UtcNow.AddMinutes(tokenExpirationInMinutesFromNow);
         context.SaveChanges();
         return(token);
     }
 }
 public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
 {
     if (string.IsNullOrEmpty(roleName))
     {
         throw CreateArgumentNullOrEmptyException("roleName");
     }
     using (BlogUnitOfWork context = new BlogUnitOfWork(new ModelContextInit()))
     {
         dynamic role = context.Roles.FirstOrDefault(Rl => Rl.RoleName == roleName);
         if (role == null)
         {
             throw new InvalidOperationException("Role not found");
         }
         if (throwOnPopulatedRole)
         {
             dynamic usersInRole = role.Users.Any;
             if (usersInRole)
             {
                 throw new InvalidOperationException(string.Format("Role populated: {0}", roleName));
             }
         }
         else
         {
             foreach (User usr_loopVariable in role.Users)
             {
                 var usr = usr_loopVariable;
                 context.Users.Remove(usr);
             }
         }
         context.Roles.Remove(role);
         context.SaveChanges();
         return(true);
     }
 }
 public override bool ResetPasswordWithToken(string token, string newPassword)
 {
     if (string.IsNullOrEmpty(newPassword))
     {
         throw CreateArgumentNullOrEmptyException("newPassword");
     }
     using (BlogUnitOfWork context = new BlogUnitOfWork(new ModelContextInit()))
     {
         dynamic user = context.Users.FirstOrDefault(Usr => Usr.PasswordVerificationToken == token && Usr.PasswordVerificationTokenExpirationDate > DateTime.Now);
         if (user != null)
         {
             dynamic newhashedPassword = CodeFirstCrypto.HashPassword(newPassword);
             if (newhashedPassword.Length > 128)
             {
                 throw new ArgumentException("Password too long");
             }
             user.Password                  = newhashedPassword;
             user.PasswordChangedDate       = DateTime.UtcNow;
             user.PasswordVerificationToken = null;
             user.PasswordVerificationTokenExpirationDate = null;
             context.SaveChanges();
             return(true);
         }
         else
         {
             return(false);
         }
     }
 }
 public override string[] GetAllRoles()
 {
     using (BlogUnitOfWork context = new BlogUnitOfWork(new ModelContextInit()))
     {
         return(context.Roles.Select(Rl => Rl.RoleName).ToArray());
     }
 }
 public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
 {
     using (BlogUnitOfWork context = new BlogUnitOfWork(new ModelContextInit()))
     {
         foreach (string username_loopVariable in usernames)
         {
             var    username = username_loopVariable;
             string us       = username;
             User   user     = context.Users.FirstOrDefault(u => u.Username == us);
             if (user != null)
             {
                 foreach (string rolename_loopVariable in roleNames)
                 {
                     var  rolename = rolename_loopVariable;
                     var  rl       = rolename;
                     Role role     = user.Roles.FirstOrDefault(r => r.RoleName == rl);
                     if (role != null)
                     {
                         user.Roles.Remove(role);
                     }
                 }
             }
         }
         context.SaveChanges();
     }
 }
        public override string CreateAccount(string userName, string password, string email, bool requireConfirmationToken)
        {
            if (string.IsNullOrEmpty(password))
            {
                throw new MembershipCreateUserException(MembershipCreateStatus.InvalidPassword);
            }
            string hashedPassword = CodeFirstCrypto.HashPassword(password);

            if (hashedPassword.Length > 128)
            {
                throw new MembershipCreateUserException(MembershipCreateStatus.InvalidPassword);
            }
            if (string.IsNullOrEmpty(userName))
            {
                throw new MembershipCreateUserException(MembershipCreateStatus.InvalidUserName);
            }
            if (string.IsNullOrEmpty(email))
            {
                throw new MembershipCreateUserException(MembershipCreateStatus.InvalidEmail);
            }
            using (BlogUnitOfWork context = new BlogUnitOfWork(new ModelContextInit()))
            {
                dynamic user      = context.Users.FirstOrDefault(Usr => Usr.Username == userName);
                dynamic emailuser = context.Users.FirstOrDefault(Usr => Usr.Email == email);
                if (user != null)
                {
                    throw new MembershipCreateUserException(MembershipCreateStatus.DuplicateUserName);
                }
                if (emailuser != null)
                {
                    throw new MembershipCreateUserException(MembershipCreateStatus.DuplicateEmail);
                }
                string token = null;
                if (requireConfirmationToken)
                {
                    token = CodeFirstCrypto.GenerateToken();
                }
                int  defaultNumPasswordFailures = 0;
                User NewUser = new User
                {
                    UserId              = Guid.NewGuid(),
                    Username            = userName,
                    Password            = hashedPassword,
                    IsConfirmed         = !requireConfirmationToken,
                    Email               = email,
                    ConfirmationToken   = token,
                    CreateDate          = DateTime.UtcNow,
                    PasswordChangedDate = DateTime.UtcNow,
                    PasswordFailuresSinceLastSuccess = defaultNumPasswordFailures,
                    LastPasswordFailureDate          = DateTime.UtcNow,
                };

                context.Users.Add(NewUser);
                context.SaveChanges();
                return(token);
            }
        }
Beispiel #8
0
 public BlogService(
     BlogUnitOfWork unitOfWork,
     IHttpContextAccessor httpContextAccessor,
     IMapper mapper
     )
 {
     _unitOfWork          = unitOfWork;
     _httpContextAccessor = httpContextAccessor;
     _mapper = mapper;
 }
 public BlogService(
     BlogUnitOfWork unitOfWork,
     IHttpContextAccessor httpContextAccessor,
     UserManager <ApplicationUser> userManager
     )
 {
     _unitOfWork          = unitOfWork;
     _userManager         = userManager;
     _httpContextAccessor = httpContextAccessor;
 }
 public override string ExtendedValidateUser(string userNameOrEmail, string password)
 {
     if (string.IsNullOrEmpty(userNameOrEmail))
     {
         throw CreateArgumentNullOrEmptyException("userNameOrEmail");
     }
     if (string.IsNullOrEmpty(password))
     {
         throw CreateArgumentNullOrEmptyException("password");
     }
     using (BlogUnitOfWork context = new BlogUnitOfWork(new ModelContextInit()))
     {
         User user = null;
         user = context.Users.FirstOrDefault(Usr => Usr.Username == userNameOrEmail);
         if (user == null)
         {
             user = context.Users.FirstOrDefault(Usr => Usr.Email == userNameOrEmail);
         }
         if (user == null)
         {
             return(string.Empty);
         }
         if (!user.IsConfirmed)
         {
             return(string.Empty);
         }
         dynamic hashedPassword        = user.Password;
         bool    verificationSucceeded = (hashedPassword != null && CodeFirstCrypto.VerifyHashedPassword(hashedPassword, password));
         if (verificationSucceeded)
         {
             user.PasswordFailuresSinceLastSuccess = 0;
         }
         else
         {
             int failures = user.PasswordFailuresSinceLastSuccess;
             if (failures != -1)
             {
                 user.PasswordFailuresSinceLastSuccess += 1;
                 user.LastPasswordFailureDate           = DateTime.UtcNow;
             }
         }
         context.SaveChanges();
         if (verificationSucceeded)
         {
             return(user.Username);
         }
         else
         {
             return(string.Empty);
         }
     }
 }
 public override bool ChangePassword(string userName, string oldPassword, string newPassword)
 {
     if (string.IsNullOrEmpty(userName))
     {
         throw CreateArgumentNullOrEmptyException("userName");
     }
     if (string.IsNullOrEmpty(oldPassword))
     {
         throw CreateArgumentNullOrEmptyException("oldPassword");
     }
     if (string.IsNullOrEmpty(newPassword))
     {
         throw CreateArgumentNullOrEmptyException("newPassword");
     }
     using (BlogUnitOfWork context = new BlogUnitOfWork(new ModelContextInit()))
     {
         dynamic user = context.Users.FirstOrDefault(Usr => Usr.Username == userName);
         if (user == null)
         {
             return(false);
         }
         dynamic hashedPassword        = user.Password;
         bool    verificationSucceeded = (hashedPassword != null && CodeFirstCrypto.VerifyHashedPassword(hashedPassword, oldPassword));
         if (verificationSucceeded)
         {
             user.PasswordFailuresSinceLastSuccess = 0;
         }
         else
         {
             int failures = user.PasswordFailuresSinceLastSuccess;
             if (failures != -1)
             {
                 user.PasswordFailuresSinceLastSuccess += 1;
                 user.LastPasswordFailureDate           = DateTime.UtcNow;
             }
             context.SaveChanges();
             return(false);
         }
         dynamic newhashedPassword = CodeFirstCrypto.HashPassword(newPassword);
         if (newhashedPassword.Length > 128)
         {
             throw new ArgumentException("Password too long");
         }
         user.Password            = newhashedPassword;
         user.PasswordChangedDate = DateTime.UtcNow;
         context.SaveChanges();
         return(true);
     }
 }
 public override string[] FindUsersInRole(string roleName, string usernameToMatch)
 {
     if (string.IsNullOrEmpty(roleName))
     {
         throw CreateArgumentNullOrEmptyException("roleName");
     }
     if (string.IsNullOrEmpty(usernameToMatch))
     {
         throw CreateArgumentNullOrEmptyException("usernameToMatch");
     }
     using (BlogUnitOfWork context = new BlogUnitOfWork(new ModelContextInit()))
     {
         var query = from Rl in context.Roles from Usr in Rl.Users where Rl.RoleName == roleName && Usr.Username.Contains(usernameToMatch) select Usr.Username;
         return(query.ToArray());
     }
 }
 public override string[] GetUsersInRole(string roleName)
 {
     if (string.IsNullOrEmpty(roleName))
     {
         throw CreateArgumentNullOrEmptyException("roleName");
     }
     using (BlogUnitOfWork context = new BlogUnitOfWork(new ModelContextInit()))
     {
         var role = context.Roles.FirstOrDefault(Rl => Rl.RoleName == roleName);
         if (role == null)
         {
             throw new InvalidOperationException("Role not found");
         }
         return(role.Users.Select(Usr => Usr.Username).ToArray());
     }
 }
 public override System.Guid GetUserIdFromPasswordResetToken(string token)
 {
     if (string.IsNullOrEmpty(token))
     {
         throw CreateArgumentNullOrEmptyException("token");
     }
     using (BlogUnitOfWork context = new BlogUnitOfWork(new ModelContextInit()))
     {
         dynamic result = context.Users.FirstOrDefault(Usr => Usr.PasswordVerificationToken == token);
         if (result != null)
         {
             return(result.UserId);
         }
         return(Guid.Empty);
     }
 }
 public override System.DateTime GetLastPasswordFailureDate(string userName)
 {
     if (string.IsNullOrEmpty(userName))
     {
         throw CreateArgumentNullOrEmptyException("userName");
     }
     using (BlogUnitOfWork context = new BlogUnitOfWork(new ModelContextInit()))
     {
         dynamic user = context.Users.FirstOrDefault(Usr => Usr.Username == userName);
         if (user == null)
         {
             throw new InvalidOperationException(string.Format("User not found: {0}", userName));
         }
         return(user.LastPasswordFailureDate);
     }
 }
 public override string[] GetRolesForUser(string userName)
 {
     if (string.IsNullOrEmpty(userName))
     {
         throw CreateArgumentNullOrEmptyException("userName");
     }
     using (BlogUnitOfWork context = new BlogUnitOfWork(new ModelContextInit()))
     {
         var user = context.Users.FirstOrDefault(Usr => Usr.Username == userName);
         if (user == null)
         {
             throw new InvalidOperationException(string.Format("User not found: {0}", userName));
         }
         return(user.Roles.Select(Rl => Rl.RoleName).ToArray());
     }
 }
 public override System.Web.Security.MembershipUser GetUser(string userName, bool userIsOnline)
 {
     if (string.IsNullOrEmpty(userName))
     {
         throw CreateArgumentNullOrEmptyException("userName");
     }
     using (BlogUnitOfWork context = new BlogUnitOfWork(new ModelContextInit()))
     {
         dynamic user = context.Users.FirstOrDefault(Usr => Usr.Username == userName);
         if (user == null)
         {
             return(null);
         }
         return(new MembershipUser(Membership.Provider.Name, userName, user.UserId, user.Email, null, null, true, false, user.CreateDate, DateTime.MinValue,
                                   DateTime.MinValue, DateTime.MinValue, DateTime.MinValue));
     }
 }
 public override bool DeleteAccount(string userName)
 {
     if (string.IsNullOrEmpty(userName))
     {
         throw CreateArgumentNullOrEmptyException("userName");
     }
     using (BlogUnitOfWork context = new BlogUnitOfWork(new ModelContextInit()))
     {
         dynamic user = context.Users.FirstOrDefault(Usr => Usr.Username == userName);
         if (user == null)
         {
             return(false);
         }
         context.Users.Remove(user);
         context.SaveChanges();
         return(true);
     }
 }
 public override bool ConfirmAccount(string accountConfirmationToken)
 {
     if (string.IsNullOrEmpty(accountConfirmationToken))
     {
         throw CreateArgumentNullOrEmptyException("accountConfirmationToken");
     }
     using (BlogUnitOfWork context = new BlogUnitOfWork(new ModelContextInit()))
     {
         dynamic row = context.Users.FirstOrDefault(Usr => Usr.ConfirmationToken == accountConfirmationToken);
         if (row != null)
         {
             row.IsConfirmed = true;
             context.SaveChanges();
             return(true);
         }
         return(false);
     }
 }
 public override bool RoleExists(string roleName)
 {
     if (string.IsNullOrEmpty(roleName))
     {
         throw CreateArgumentNullOrEmptyException("roleName");
     }
     using (BlogUnitOfWork context = new BlogUnitOfWork(new ModelContextInit()))
     {
         dynamic result = context.Roles.FirstOrDefault(Rl => Rl.RoleName == roleName);
         if (result != null)
         {
             return(true);
         }
         else
         {
             return(false);
         }
     }
 }
 public override void AddUsersToRoles(string[] usernames, string[] roleNames)
 {
     using (BlogUnitOfWork context = new BlogUnitOfWork(new ModelContextInit()))
             {
                 var users = context.Users.Where(usr => usernames.Contains(usr.Username)).ToList();
                 var roles = context.Roles.Where(rl => roleNames.Contains(rl.RoleName)).ToList();
                 foreach (User user_loopVariable in users)
                 {
                    var user = user_loopVariable;
                     foreach (Role role_loopVariable in roles)
                     {
                       var  role = role_loopVariable;
                         if (!user.Roles.Contains(role))
                         {
                             user.Roles.Add(role);
                         }
                     }
                 }
                 context.SaveChanges();
             }
 }
 public override void AddUsersToRoles(string[] usernames, string[] roleNames)
 {
     using (BlogUnitOfWork context = new BlogUnitOfWork(new ModelContextInit()))
     {
         var users = context.Users.Where(usr => usernames.Contains(usr.Username)).ToList();
         var roles = context.Roles.Where(rl => roleNames.Contains(rl.RoleName)).ToList();
         foreach (User user_loopVariable in users)
         {
             var user = user_loopVariable;
             foreach (Role role_loopVariable in roles)
             {
                 var role = role_loopVariable;
                 if (!user.Roles.Contains(role))
                 {
                     user.Roles.Add(role);
                 }
             }
         }
         context.SaveChanges();
     }
 }
 public override void CreateRole(string roleName)
 {
     if (string.IsNullOrEmpty(roleName))
             {
                 throw CreateArgumentNullOrEmptyException("roleName");
             }
             using (BlogUnitOfWork context = new BlogUnitOfWork(new ModelContextInit()))
             {
                 dynamic role = context.Roles.FirstOrDefault(Rl => Rl.RoleName == roleName);
                 if (role != null)
                 {
                     throw new InvalidOperationException(string.Format("Role exists: {0}", roleName));
                 }
                 Role NewRole = new Role
                 {
                     RoleId = Guid.NewGuid(),
                     RoleName = roleName
                 };
                 context.Roles.Add(NewRole);
                 context.SaveChanges();
             }
 }
 public override void CreateRole(string roleName)
 {
     if (string.IsNullOrEmpty(roleName))
     {
         throw CreateArgumentNullOrEmptyException("roleName");
     }
     using (BlogUnitOfWork context = new BlogUnitOfWork(new ModelContextInit()))
     {
         dynamic role = context.Roles.FirstOrDefault(Rl => Rl.RoleName == roleName);
         if (role != null)
         {
             throw new InvalidOperationException(string.Format("Role exists: {0}", roleName));
         }
         Role NewRole = new Role
         {
             RoleId   = Guid.NewGuid(),
             RoleName = roleName
         };
         context.Roles.Add(NewRole);
         context.SaveChanges();
     }
 }
 public override bool IsConfirmed(string userName)
 {
     if (string.IsNullOrEmpty(userName))
     {
         throw CreateArgumentNullOrEmptyException("userName");
     }
     using (BlogUnitOfWork context = new BlogUnitOfWork(new ModelContextInit()))
     {
         dynamic user = context.Users.FirstOrDefault(Usr => Usr.Username == userName);
         if (user == null)
         {
             return(false);
         }
         if (user.IsConfirmed)
         {
             return(true);
         }
         else
         {
             return(false);
         }
     }
 }
Beispiel #26
0
 public AuthorRepository(BlogUnitOfWork uow) : base(uow)
 {
 }
 public override string[] FindUsersInRole(string roleName, string usernameToMatch)
 {
     if (string.IsNullOrEmpty(roleName)) {
                 throw CreateArgumentNullOrEmptyException("roleName");
             }
             if (string.IsNullOrEmpty(usernameToMatch)) {
                 throw CreateArgumentNullOrEmptyException("usernameToMatch");
             }
             using (BlogUnitOfWork context = new BlogUnitOfWork(new ModelContextInit()))
             {
                 var query = from Rl in context.Roles from Usr in Rl.Users where Rl.RoleName == roleName && Usr.Username.Contains(usernameToMatch) select Usr.Username;
                 return query.ToArray();
             }
 }
Beispiel #28
0
 public PostRepository(BlogUnitOfWork uow)
 {
     _uow        = uow;
     _collection = _uow.Database.GetCollection <Post>($"{nameof(Post)}s");
 }
 public override int GetPasswordFailuresSinceLastSuccess(string userName)
 {
     if (string.IsNullOrEmpty(userName))
             {
                 throw CreateArgumentNullOrEmptyException("userName");
             }
             using (BlogUnitOfWork context = new BlogUnitOfWork(new ModelContextInit()))
             {
                 dynamic user = context.Users.FirstOrDefault(Usr => Usr.Username == userName);
                 if (user == null)
                 {
                     throw new InvalidOperationException(string.Format("User not found: {0}", userName));
                 }
                 return user.PasswordFailuresSinceLastSuccess;
             }
 }
 public override string GeneratePasswordResetToken(string userName, int tokenExpirationInMinutesFromNow)
 {
     if (string.IsNullOrEmpty(userName))
             {
                 throw CreateArgumentNullOrEmptyException("userName");
             }
             using (BlogUnitOfWork context = new BlogUnitOfWork(new ModelContextInit()))
             {
                 dynamic user = context.Users.FirstOrDefault(Usr => Usr.Username == userName);
                 if (user == null)
                 {
                     throw new InvalidOperationException("El usuario no existe");
                 }
                 if (!user.IsConfirmed)
                 {
                     throw new InvalidOperationException("El usuario no existe");
                 }
                 string token = null;
                 if (user.PasswordVerificationTokenExpirationDate > DateTime.UtcNow)
                 {
                     token = user.PasswordVerificationToken;
                 }
                 else
                 {
                     token = CodeFirstCrypto.GenerateToken();
                 }
                 user.PasswordVerificationToken = token;
                 user.PasswordVerificationTokenExpirationDate = DateTime.UtcNow.AddMinutes(tokenExpirationInMinutesFromNow);
                 context.SaveChanges();
                 return token;
             }
 }
 public override bool DeleteAccount(string userName)
 {
     if (string.IsNullOrEmpty(userName))
             {
                 throw CreateArgumentNullOrEmptyException("userName");
             }
             using (BlogUnitOfWork context = new BlogUnitOfWork(new ModelContextInit()))
             {
                 dynamic user = context.Users.FirstOrDefault(Usr => Usr.Username == userName);
                 if (user == null)
                 {
                     return false;
                 }
                 context.Users.Remove(user);
                 context.SaveChanges();
                 return true;
             }
 }
 public override bool ConfirmAccount(string accountConfirmationToken)
 {
     if (string.IsNullOrEmpty(accountConfirmationToken))
             {
                 throw CreateArgumentNullOrEmptyException("accountConfirmationToken");
             }
             using (BlogUnitOfWork context = new BlogUnitOfWork(new ModelContextInit()))
             {
                 dynamic row = context.Users.FirstOrDefault(Usr => Usr.ConfirmationToken == accountConfirmationToken);
                 if (row != null)
                 {
                     row.IsConfirmed = true;
                     context.SaveChanges();
                     return true;
                 }
                 return false;
             }
 }
 public override bool RoleExists(string roleName)
 {
     if (string.IsNullOrEmpty(roleName))
             {
                 throw CreateArgumentNullOrEmptyException("roleName");
             }
             using (BlogUnitOfWork context = new BlogUnitOfWork(new ModelContextInit()))
             {
                 dynamic result = context.Roles.FirstOrDefault(Rl => Rl.RoleName == roleName);
                 if (result != null)
                 {
                     return true;
                 }
                 else
                 {
                     return false;
                 }
             }
 }
Beispiel #34
0
 public RepositoryBase(BlogUnitOfWork uow)
 {
     _uow = uow;
 }
 public override bool ResetPasswordWithToken(string token, string newPassword)
 {
     if (string.IsNullOrEmpty(newPassword))
             {
                 throw CreateArgumentNullOrEmptyException("newPassword");
             }
             using (BlogUnitOfWork context = new BlogUnitOfWork(new ModelContextInit()))
             {
                 dynamic user = context.Users.FirstOrDefault(Usr => Usr.PasswordVerificationToken == token && Usr.PasswordVerificationTokenExpirationDate > DateTime.Now);
                 if (user != null)
                 {
                     dynamic newhashedPassword = CodeFirstCrypto.HashPassword(newPassword);
                     if (newhashedPassword.Length > 128)
                     {
                         throw new ArgumentException("Password too long");
                     }
                     user.Password = newhashedPassword;
                     user.PasswordChangedDate = DateTime.UtcNow;
                     user.PasswordVerificationToken = null;
                     user.PasswordVerificationTokenExpirationDate = null;
                     context.SaveChanges();
                     return true;
                 }
                 else
                 {
                     return false;
                 }
             }
 }
 public override System.Guid GetUserIdFromPasswordResetToken(string token)
 {
     if (string.IsNullOrEmpty(token))
             {
                 throw CreateArgumentNullOrEmptyException("token");
             }
             using (BlogUnitOfWork context = new BlogUnitOfWork(new ModelContextInit()))
             {
                 dynamic result = context.Users.FirstOrDefault(Usr => Usr.PasswordVerificationToken == token);
                 if (result != null)
                 {
                     return result.UserId;
                 }
                 return Guid.Empty;
             }
 }
Beispiel #37
0
 public AuthorRepository(BlogUnitOfWork uow)
 {
     _uow        = uow;
     _collection = _uow.Database.GetCollection <Author>($"{nameof(Author)}s");
 }
 public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
 {
     using (BlogUnitOfWork context = new BlogUnitOfWork(new ModelContextInit()))
             {
                 foreach (string username_loopVariable in usernames)
                 {
                    var username = username_loopVariable;
                     string us = username;
                     User user = context.Users.FirstOrDefault(u => u.Username == us);
                     if (user != null)
                     {
                         foreach (string rolename_loopVariable in roleNames)
                         {
                            var rolename = rolename_loopVariable;
                             var rl = rolename;
                             Role role = user.Roles.FirstOrDefault(r => r.RoleName == rl);
                             if (role != null)
                             {
                                 user.Roles.Remove(role);
                             }
                         }
                     }
                 }
                 context.SaveChanges();
             }
 }
 public override bool ChangePassword(string userName, string oldPassword, string newPassword)
 {
     if (string.IsNullOrEmpty(userName))
             {
                 throw CreateArgumentNullOrEmptyException("userName");
             }
             if (string.IsNullOrEmpty(oldPassword))
             {
                 throw CreateArgumentNullOrEmptyException("oldPassword");
             }
             if (string.IsNullOrEmpty(newPassword))
             {
                 throw CreateArgumentNullOrEmptyException("newPassword");
             }
             using (BlogUnitOfWork context = new BlogUnitOfWork(new ModelContextInit()))
             {
                 dynamic user = context.Users.FirstOrDefault(Usr => Usr.Username == userName);
                 if (user == null)
                 {
                     return false;
                 }
                 dynamic hashedPassword = user.Password;
                 bool verificationSucceeded = (hashedPassword != null && CodeFirstCrypto.VerifyHashedPassword(hashedPassword, oldPassword));
                 if (verificationSucceeded)
                 {
                     user.PasswordFailuresSinceLastSuccess = 0;
                 }
                 else
                 {
                     int failures = user.PasswordFailuresSinceLastSuccess;
                     if (failures != -1)
                     {
                         user.PasswordFailuresSinceLastSuccess += 1;
                         user.LastPasswordFailureDate = DateTime.UtcNow;
                     }
                     context.SaveChanges();
                     return false;
                 }
                 dynamic newhashedPassword = CodeFirstCrypto.HashPassword(newPassword);
                 if (newhashedPassword.Length > 128)
                 {
                     throw new ArgumentException("Password too long");
                 }
                 user.Password = newhashedPassword;
                 user.PasswordChangedDate = DateTime.UtcNow;
                 context.SaveChanges();
                 return true;
             }
 }
 public override bool IsUserInRole(string userName, string roleName)
 {
     if (string.IsNullOrEmpty(userName))
             {
                 throw CreateArgumentNullOrEmptyException("userName");
             }
             if (string.IsNullOrEmpty(roleName))
             {
                 throw CreateArgumentNullOrEmptyException("roleName");
             }
             using (BlogUnitOfWork context = new BlogUnitOfWork(new ModelContextInit()))
             {
                 dynamic user = context.Users.FirstOrDefault(Usr => Usr.Username == userName);
                 if (user == null)
                 {
                     return false;
                 }
                 dynamic role = context.Roles.FirstOrDefault(Rl => Rl.RoleName == roleName);
                 if (role == null)
                 {
                     return false;
                 }
                 return user.Roles.Contains(role);
             }
 }
        public override string CreateAccount(string userName, string password, string email, string firstname, string lastname, string timezone, string culture, bool requireConfirmationToken)
        {
            if (string.IsNullOrEmpty(password))
                    {
                        throw new MembershipCreateUserException(MembershipCreateStatus.InvalidPassword);
                    }
                    string hashedPassword = CodeFirstCrypto.HashPassword(password);
                    if (hashedPassword.Length > 128)
                    {
                        throw new MembershipCreateUserException(MembershipCreateStatus.InvalidPassword);
                    }
                    if (string.IsNullOrEmpty(userName))
                    {
                        throw new MembershipCreateUserException(MembershipCreateStatus.InvalidUserName);
                    }
                    if (string.IsNullOrEmpty(email))
                    {
                        throw new MembershipCreateUserException(MembershipCreateStatus.InvalidEmail);
                    }
                    using (BlogUnitOfWork context = new BlogUnitOfWork(new ModelContextInit()))
                    {
                        dynamic user = context.Users.FirstOrDefault(Usr => Usr.Username == userName);
                        dynamic emailuser = context.Users.FirstOrDefault(Usr => Usr.Email == email);
                        if (user != null)
                        {
                            throw new MembershipCreateUserException(MembershipCreateStatus.DuplicateUserName);
                        }
                        if (emailuser != null)
                        {
                            throw new MembershipCreateUserException(MembershipCreateStatus.DuplicateEmail);
                        }
                        string token = null;
                        if (requireConfirmationToken)
                        {
                            token = CodeFirstCrypto.GenerateToken();
                        }
                        int defaultNumPasswordFailures = 0;
                        User NewUser = new User
                        {
                            UserId = Guid.NewGuid(),
                            Username = userName,
                            Password = hashedPassword,
                            IsConfirmed = !requireConfirmationToken,
                            Email = email,
                            ConfirmationToken = token,
                            CreateDate = DateTime.UtcNow,
                            PasswordChangedDate = DateTime.UtcNow,
                            PasswordFailuresSinceLastSuccess = defaultNumPasswordFailures,
                            LastPasswordFailureDate = DateTime.UtcNow,
                            Culture = culture,
                            TimeZone = timezone,
                            FirstName = firstname,
                            LastName = lastname,
                        };

                        context.Users.Add(NewUser);
                        context.SaveChanges();
                        return token;
                    }
        }
 public override string[] GetUsersInRole(string roleName)
 {
     if (string.IsNullOrEmpty(roleName))
             {
                 throw CreateArgumentNullOrEmptyException("roleName");
             }
             using (BlogUnitOfWork context = new BlogUnitOfWork(new ModelContextInit()))
             {
                 var role = context.Roles.FirstOrDefault(Rl => Rl.RoleName == roleName);
                 if (role == null)
                 {
                     throw new InvalidOperationException("Role not found");
                 }
                 return role.Users.Select(Usr => Usr.Username).ToArray();
             }
 }
 public override string ExtendedValidateUser(string userNameOrEmail, string password)
 {
     if (string.IsNullOrEmpty(userNameOrEmail))
             {
                 throw CreateArgumentNullOrEmptyException("userNameOrEmail");
             }
             if (string.IsNullOrEmpty(password))
             {
                 throw CreateArgumentNullOrEmptyException("password");
             }
             using (BlogUnitOfWork context = new BlogUnitOfWork(new ModelContextInit()))
             {
                 User user = null;
                 user = context.Users.FirstOrDefault(Usr => Usr.Username == userNameOrEmail);
                 if (user == null)
                 {
                     user = context.Users.FirstOrDefault(Usr => Usr.Email == userNameOrEmail);
                 }
                 if (user == null)
                 {
                     return string.Empty;
                 }
                 if (!user.IsConfirmed)
                 {
                     return string.Empty;
                 }
                 dynamic hashedPassword = user.Password;
                 bool verificationSucceeded = (hashedPassword != null && CodeFirstCrypto.VerifyHashedPassword(hashedPassword, password));
                 if (verificationSucceeded)
                 {
                     user.PasswordFailuresSinceLastSuccess = 0;
                 }
                 else
                 {
                     int failures = user.PasswordFailuresSinceLastSuccess;
                     if (failures != -1)
                     {
                         user.PasswordFailuresSinceLastSuccess += 1;
                         user.LastPasswordFailureDate = DateTime.UtcNow;
                     }
                 }
                 context.SaveChanges();
                 if (verificationSucceeded)
                 {
                     return user.Username;
                 }
                 else
                 {
                     return string.Empty;
                 }
             }
 }
 public override string[] GetRolesForUser(string userName)
 {
     if (string.IsNullOrEmpty(userName))
             {
                 throw CreateArgumentNullOrEmptyException("userName");
             }
             using (BlogUnitOfWork context = new BlogUnitOfWork(new ModelContextInit()))
             {
                 var user = context.Users.FirstOrDefault(Usr => Usr.Username == userName);
                 if (user == null)
                 {
                     throw new InvalidOperationException(string.Format("User not found: {0}", userName));
                 }
                 return user.Roles.Select(Rl => Rl.RoleName).ToArray();
             }
 }
 public override System.DateTime GetCreateDate(string userName)
 {
     if (string.IsNullOrEmpty(userName))
             {
                 throw CreateArgumentNullOrEmptyException("userName");
             }
             using (BlogUnitOfWork context = new BlogUnitOfWork(new ModelContextInit()))
             {
                 dynamic user = context.Users.FirstOrDefault(Usr => Usr.Username == userName);
                 if (user == null)
                 {
                     throw new InvalidOperationException(string.Format("User not found: {0}", userName));
                 }
                 return user.CreateDate;
             }
 }
 public override string[] GetAllRoles()
 {
     using (BlogUnitOfWork context = new BlogUnitOfWork(new ModelContextInit()))
             {
                 return context.Roles.Select(Rl => Rl.RoleName).ToArray();
             }
 }
 public override System.Web.Security.MembershipUser GetUser(string userName, bool userIsOnline)
 {
     if (string.IsNullOrEmpty(userName))
             {
                 throw CreateArgumentNullOrEmptyException("userName");
             }
             using (BlogUnitOfWork context = new BlogUnitOfWork(new ModelContextInit()))
             {
                 dynamic user = context.Users.FirstOrDefault(Usr => Usr.Username == userName);
                 if (user == null)
                 {
                     return null;
                 }
                 return new MembershipUser(Membership.Provider.Name, userName, user.UserId, user.Email, null, null, true, false, user.CreateDate, DateTime.MinValue,
                 DateTime.MinValue, DateTime.MinValue, DateTime.MinValue);
             }
 }
 public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
 {
     if (string.IsNullOrEmpty(roleName))
             {
                 throw CreateArgumentNullOrEmptyException("roleName");
             }
             using (BlogUnitOfWork context = new BlogUnitOfWork(new ModelContextInit()))
             {
                 dynamic role = context.Roles.FirstOrDefault(Rl => Rl.RoleName == roleName);
                 if (role == null)
                 {
                     throw new InvalidOperationException("Role not found");
                 }
                 if (throwOnPopulatedRole)
                 {
                     dynamic usersInRole = role.Users.Any;
                     if (usersInRole)
                     {
                         throw new InvalidOperationException(string.Format("Role populated: {0}", roleName));
                     }
                 }
                 else
                 {
                     foreach (User usr_loopVariable in role.Users)
                     {
                        var usr = usr_loopVariable;
                        context.Users.Remove(usr);
                     }
                 }
                 context.Roles.Remove(role);
                 context.SaveChanges();
                 return true;
             }
 }
 public override bool IsConfirmed(string userName)
 {
     if (string.IsNullOrEmpty(userName))
             {
                 throw CreateArgumentNullOrEmptyException("userName");
             }
             using (BlogUnitOfWork context = new BlogUnitOfWork(new ModelContextInit()))
             {
                 dynamic user = context.Users.FirstOrDefault(Usr => Usr.Username == userName);
                 if (user == null)
                 {
                     return false;
                 }
                 if (user.IsConfirmed)
                 {
                     return true;
                 }
                 else
                 {
                     return false;
                 }
             }
 }
Beispiel #50
0
 public PostRepository(BlogUnitOfWork uow) : base(uow)
 {
     _uow = uow;
 }
Beispiel #51
0
 public BlogSettingsRepository(BlogUnitOfWork uow)
 {
     _uow        = uow;
     _collection = _uow.Database.GetCollection <BlogSettings>(nameof(BlogSettings));
 }
 public BlogSettingsRepository(BlogUnitOfWork uow) : base(uow) { }