示例#1
0
        public User AddUser(string name, string surname, string emailAddress, string cellphoneNumber, string password)
        {
            if (_recipeManagementContext.Users.AsEnumerable().Any(u => u.EmailAddress == emailAddress))
            {
                throw new RecipeManagementException("User with specified email address already exists");
            }

            var user = new User
            {
                UserId = Guid.NewGuid(),
                Name = name,
                Surname = surname,
                CellphoneNumber = cellphoneNumber,
                EmailAddress = emailAddress,
                PasswordSalt = _cryptographyService.CreateSalt()
            };
            user.Password = _cryptographyService.CreatePasswordHash(password, user.PasswordSalt);
            DateTime now = DateTime.Now;
            user.DateCreated = now;
            user.DateUpdated = now;

            _recipeManagementContext.Users.Add(user);

            _recipeManagementContext.SaveChanges();

            return user;
        }
示例#2
0
        public UserRegistrationStatus Register(string email, string password, PasswordFormat passwordFormat)
        {
            //before registering the user, we need to check a few things

            //does the user exist already?
            var existingUser = _userService.FirstOrDefault(x => x.Email == email);

            if (existingUser != null)
            {
                return(UserRegistrationStatus.FailedAsEmailAlreadyExists);
            }

            //we can create a user now, we'll need to hash the password
            var salt = _cryptographyService.CreateSalt(8); //64 bits...should be good enough

            var hashedPassword = _cryptographyService.GetHashedPassword(password, salt, passwordFormat);

            //create a new user entity
            var user = new User()
            {
                Email               = email,
                UserName            = email,
                CreatedOnUtc        = DateTime.UtcNow,
                LastActivityDateUtc = DateTime.UtcNow,
                LastLoginDateUtc    = DateTime.UtcNow,
                LastIpAddress       = _webHelper.GetCurrentIpAddress(),
                Password            = hashedPassword,
                PasswordSalt        = salt,
                PasswordFormat      = passwordFormat,
                IsAdmin             = false,
                IsSystemAccount     = false,
                Active              = true,
                Guid = Guid.NewGuid()
            };

            _userService.Insert(user);
            return(UserRegistrationStatus.Success);
        }
示例#3
0
        public async Task <User> CreateUser(string _userName, string _password, string _email)
        {
            User current = new User();

            current.Username = _userName;

            byte[] salt = _cryptographyService.CreateSalt();
            current.Password = _cryptographyService.GenerateHash(_password, salt);
            current.Email    = _email;
            current.Salt     = Convert.ToBase64String(salt);

            current.Id = await _userRepository.AddUser(current);

            return(current);
        }
        public IHttpActionResult Post(UserEntityModel entityModel)
        {
            User user;

            user = entityModel.Id == 0 ? new User() : _userService.Get(entityModel.Id);

            if (user == null)
            {
                return(NotFound());
            }

            //check if the email has already been registered
            var emailUser = _userService.Get(x => x.Email == entityModel.Email, earlyLoad: x => x.UserRoles.Select(y => y.Role)).FirstOrDefault();

            if (emailUser != null && emailUser.Id != user.Id)
            {
                VerboseReporter.ReportError("The email is already registered with another user", "post_user");
                return(RespondFailure());
            }

            //same for user name
            if (_userSettings.AreUserNamesEnabled)
            {
                var userNameUser = _userService.Get(x => x.UserName == entityModel.UserName, null).FirstOrDefault();
                if (userNameUser != null && userNameUser.Id != user.Id)
                {
                    VerboseReporter.ReportError("The username is already taken by another user", "post_user");
                    return(RespondFailure());
                }
            }

            //we should have at least one role
            if (entityModel.RoleIds.Count == 0)
            {
                VerboseReporter.ReportError("At least one role must be assigned to the user", "post_user");
                return(RespondFailure());
            }
            //is this a new user, we'll require password
            if (string.IsNullOrEmpty(entityModel.Password) && entityModel.Id == 0)
            {
                VerboseReporter.ReportError("You must specify the password for the user", "post_user");
                return(RespondFailure());
            }
            //are passwords same?
            if (!string.IsNullOrEmpty(entityModel.Password) && string.Compare(entityModel.Password, entityModel.ConfirmPassword, StringComparison.Ordinal) != 0)
            {
                VerboseReporter.ReportError("The passwords do not match", "post_user");
                return(RespondFailure());
            }

            user.FirstName   = entityModel.FirstName;
            user.LastName    = entityModel.LastName;
            user.Email       = entityModel.Email;
            user.Remarks     = entityModel.Remarks;
            user.Active      = entityModel.Active;
            user.DateUpdated = DateTime.UtcNow;
            user.Name        = string.Concat(user.FirstName, " ", user.LastName);
            user.UserName    = entityModel.UserName;

            if (!string.IsNullOrEmpty(entityModel.Password)) // update password if provided
            {
                if (string.IsNullOrEmpty(user.PasswordSalt))
                {
                    user.PasswordSalt = _cryptographyService.CreateSalt(8);
                }
                user.Password = _cryptographyService.GetHashedPassword(entityModel.Password, user.PasswordSalt,
                                                                       _securitySettings.DefaultPasswordStorageFormat);
            }

            _userService.Update(user);

            //assign the roles now
            var roles = _roleService.Get(x => x.IsActive);
            //current roles
            var currentRoleIds = user.UserRoles.Select(x => x.RoleId).ToList();
            //roles to unassign
            var rolesToUnassign = currentRoleIds.Except(entityModel.RoleIds);

            foreach (var roleId in rolesToUnassign)
            {
                var role = roles.FirstOrDefault(x => x.Id == roleId);
                if (role == null)
                {
                    continue;
                }

                _roleService.UnassignRoleToUser(role, user);
            }

            //roles to assign
            var rolesToAssign = entityModel.RoleIds.Except(currentRoleIds);

            foreach (var roleId in rolesToAssign)
            {
                var role = roles.FirstOrDefault(x => x.Id == roleId);
                if (role == null)
                {
                    continue;
                }

                _roleService.AssignRoleToUser(role, user);
            }

            //any images to assign
            if (entityModel.CoverImageId != 0)
            {
                user.SetPropertyValue(PropertyNames.DefaultCoverId, entityModel.CoverImageId);
            }
            if (entityModel.ProfileImageId != 0)
            {
                user.SetPropertyValue(PropertyNames.DefaultPictureId, entityModel.ProfileImageId);
            }

            VerboseReporter.ReportSuccess("User saved successfully", "post_user");
            return(RespondSuccess(new {
                User = user.ToEntityModel(_mediaService, _mediaSettings)
            }));
        }