public UpdateResult CreateRoleForUser(User user, string role)
        {
            if (user == null)
                throw new ArgumentNullException("user");

            return CreateRoleForUser(user.Email, role, user.Stamp);
        }
Example #2
0
 public Identity(User user, bool isAuthenticated)
 {
     _isAuthenticated = isAuthenticated && user != null;
     _name = user == null ? string.Empty : user.Name;
     _email = user == null ? string.Empty : user.Email;
     _userId = user == null ? 0 : user.Id;
     _locale = user == null ? null : user.Locale;
 }
Example #3
0
        public ResetPasswordRequest CreatePasswordResetRequestForUser(User user, DateTime expiration)
        {
            if (user == null)
                throw new ArgumentNullException("user");

            ResetPasswordRequest request = new ResetPasswordRequest
            {
                Token = Guid.NewGuid().ToString(),
                UserId = user.Id,
                Expiration = expiration
            };

            return _resetPasswdRequestsRepository.CreateRequest(request);
        }
Example #4
0
        public void CreateRoleForUser(User user, string role)
        {
            if (user == null)
                throw new ArgumentNullException("user");

            if (role == null)
                throw new ArgumentNullException("role");

            UpdateResult updateResult = _userRoleRepository.CreateRoleForUser(user, role);
            switch (updateResult)
            {
                case UpdateResult.Stalled:
                    throw new StaleUserException(string.Format(CultureInfo.InvariantCulture, "Error creating user role for user with email {0}, object stalled", user.Email));
                case UpdateResult.ItemNotExists:
                    throw new UserNotExistsException(string.Format(CultureInfo.InvariantCulture, "Error creating user role for user with email {0}, user not found", user.Email));
            }
        }
        public User Insert(User user)
        {
            if (user == null)
                throw new ArgumentNullException("user");

            return SqlQueryExecutor.Execute(() =>
            {
                String query = string.Format(CultureInfo.InvariantCulture, "INSERT INTO {1} {0} RETURNING id;", InsertFieldList, TableName);
                int insertedId = _connectionProvider.CurrentConnection.Query<int>(query, new { user.Name, user.Email, user.Password, user.Locale, user.TimeZone }).FirstOrDefault();
                if (insertedId <= 0)
                    throw new MonsterDbException("User insertion failed");

                String idAndStampQuery = string.Format(CultureInfo.InvariantCulture, "SELECT id AS Id, stamp AS Stamp FROM {0} WHERE id=@Id;", TableName);
                IdAndStamp idAndStamp = _connectionProvider.CurrentConnection.Query<IdAndStamp>(idAndStampQuery, new { Id = insertedId }).FirstOrDefault();

                if (idAndStamp == null)
                    throw new MonsterDbException("User insertion failed");

                user.Id = idAndStamp.Id;
                user.Stamp = idAndStamp.Stamp;
                return user;
            });
        }
        public IEnumerable<UserRole> GetRolesForUser(User user)
        {
            if (user == null)
                throw new ArgumentNullException("user");

            return GetRolesForUser(user.Id);
        }
        private bool ValidatePasswords(User user, string oldPasswordCheck)
        {
            if (user == null)
                throw new ArgumentNullException("user");

            string salt;
            try
            {
                salt = _passwordHasher.GetSaltFromHash(user.Password);
            }
            catch (MonsterSecurityException ex)
            {
                Logger.ErrorFormat(CultureInfo.InvariantCulture, "User with email {0} has invalid password hash", ex, user.Email);
                return false;
            }

            string checkHash = _passwordHasher.EncryptPassword(oldPasswordCheck, salt);

            return user.Password.Equals(checkHash, StringComparison.InvariantCulture);
        }
        public MembershipCreateStatus CreateUser(string email, string password, string userName, string locale, int timezone)
        {
            if (!IsUserEmailValid(ref email))
                return MembershipCreateStatus.InvalidEmail;

            if (!IsUserNameValid(ref userName))
                return MembershipCreateStatus.InvalidUserName;

            var userCheck = _userService.GetUserByEmail(email);
            if (userCheck != null)
                return MembershipCreateStatus.DuplicateEmail;

            if (!IsUserPasswordValid(ref password))
                return MembershipCreateStatus.InvalidPassword;

            User user = new User
                {
                    Email = email,
                    Name = userName,
                    Password = _passwordHasher.EncryptPassword(password),
                    Locale = _localeProvider.GetCultureByNameOrDefault(locale).ShortName,
                    TimeZone = _timeZonesProvider.GetTimeZoneByIdOrDefault(timezone).Id
                };

            try
            {
                _userService.Insert(user);
                _userService.CreateRoleForUser(user, Constants.RoleUser);
            }
            catch (Exception ex)
            {
                Logger.ErrorFormat(CultureInfo.InvariantCulture, "Error creating user with email {0}", ex, user.Email);
                return MembershipCreateStatus.ProviderError;
            }
            return MembershipCreateStatus.Success;
        }
 private ProfilePasswordModel InitProfilePasswordModel(ref ProfilePasswordModel passwdModel, User user)
 {
     if (passwdModel == null)
         passwdModel = new ProfilePasswordModel();
     if (user != null)
         passwdModel.Stamp = user.Stamp.ToBinary();
     passwdModel.Init(_membershipService.MinPasswordLength);
     return passwdModel;
 }
        private ProfileBaseModel InitBaseProfileModel(ref ProfileBaseModel baseModel, User user)
        {
            if (baseModel == null)
                baseModel = new ProfileBaseModel();

            if (user != null)
            {
                baseModel.Email = user.Email;
                baseModel.UserName = user.Name;
                baseModel.Locale = user.Locale;
                baseModel.Stamp = user.Stamp.ToBinary();
                baseModel.TimeZone = user.TimeZone;
            }

            ProfileBaseModel tmpModel = baseModel;
            baseModel.Init(GetSupportedLocales(), GetSupportedTimeZones());
            var selectedLocale = baseModel.SupportedLocales.Where(l => String.Equals(l.Value, tmpModel.Locale, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
            if (selectedLocale != null)
                selectedLocale.Selected = true;
            return baseModel;
        }
        public UpdateResult Update(User user)
        {
            if (user == null)
                throw new ArgumentNullException("user");

            return SqlQueryExecutor.Execute(() =>
            {
                return _connectionProvider.DoInTransaction(() =>
                {
                    String checkStampQuery = string.Format(CultureInfo.InvariantCulture, "SELECT {0} FROM {1} u " +
                    "WHERE u.Id = @Id AND u.stamp = @Stamp LIMIT 1", SelectFieldList, TableName);
                    User userCheck = _connectionProvider.CurrentConnection.Query<User>(checkStampQuery, new { user.Id, user.Stamp }).FirstOrDefault();

                    if (userCheck == null)
                    {
                        String checkUserQuery = string.Format(CultureInfo.InvariantCulture, "SELECT {0} FROM {1} u " +
                        "WHERE u.Id = @Id LIMIT 1", SelectFieldList, TableName);
                        userCheck = _connectionProvider.CurrentConnection.Query<User>(checkUserQuery, new { user.Id }).FirstOrDefault();
                        return userCheck == null ? UpdateResult.ItemNotExists : UpdateResult.Stalled;
                    }

                    String query = string.Format(CultureInfo.InvariantCulture, "UPDATE {1} SET {0} WHERE Id=@Id", UpdateFieldList, TableName);
                    _connectionProvider.CurrentConnection.Execute(query, new { user.Email, user.Name, user.Password, user.Locale, user.TimeZone, user.Id });

                    String idAndStampQuery = string.Format(CultureInfo.InvariantCulture, "SELECT id AS Id, stamp AS Stamp FROM {0} WHERE id=@Id;", TableName);
                    IdAndStamp idAndStamp = _connectionProvider.CurrentConnection.Query<IdAndStamp>(idAndStampQuery, new { user.Id }).FirstOrDefault();

                    if (idAndStamp == null)
                        throw new MonsterDbException("User update failed");

                    user.Id = idAndStamp.Id;
                    user.Stamp = idAndStamp.Stamp;
                    return UpdateResult.Success;
                });
            });
        }
        public User Load(User user)
        {
            if (user == null)
                throw new ArgumentNullException("user");

            return Load(user.Id);
        }
Example #13
0
 public IEnumerable<UserRole> GetRolesForUser(User user)
 {
     return _userRoleRepository.GetRolesForUser(user);
 }
Example #14
0
        public void UpdateUser(User user)
        {
            if (user == null)
                throw new ArgumentNullException("user");

            UpdateResult updateResult = _userRepository.Update(user);
            switch (updateResult)
            {
                case UpdateResult.Stalled:
                    throw new StaleUserException(user.Id);
                case UpdateResult.ItemNotExists:
                    throw new UserNotExistsException(user.Id);
            }
        }
Example #15
0
 public User Insert(User user)
 {
     return _userRepository.Insert(user);
 }
 private static void SetUser(HttpContext context, User user, string[] roles, bool isAutheticated)
 {
     Identity identity = new Identity(user, isAutheticated);
     Principal principal = new Principal(identity, roles);
     context.User = principal;
 }