예제 #1
0
        public IDtoOutObjects CreateUser(DtoInUser dtoInUser)
        {
            string  deviceName = dtoInUser.DeviceName;
            User    user       = new User();
            var     configIn   = new MapperConfiguration(cfg => { cfg.CreateMap <DtoInUser, User>(); });
            IMapper mapperIn   = configIn.CreateMapper();

            mapperIn.Map(dtoInUser, user);
            user.BornDate = new DateTime(dtoInUser.Year, dtoInUser.Month, dtoInUser.Day);
            try
            {
                if (!UserExists(user))
                {
                    Credential c = new Credential(user.Email, Guid.NewGuid().ToString(), user);

                    Credential credential = credentialsRepository.Add(c);
                    credentialsRepository.Save();
                    User u = _usersRepostiory.FindBy(x => x.Id == user.Id && x.IsDeleted == false).FirstOrDefault();
                    #region createMapperUser
                    var     config = new MapperConfiguration(cfg => { cfg.CreateMap <User, DtoOutUser>(); });
                    IMapper mapper = config.CreateMapper();
                    #endregion
                    DtoOutUser dtoOutUser = new DtoOutUser();
                    mapper.Map(u, dtoOutUser);

                    dtoOutUser.TokenString = TokenTools.CreateToken(u, deviceName).TokenString;
                    return(dtoOutUser);
                }
                else
                {
                    DtoOutError error = new DtoOutError();
                    error.Exception = new DuplicateObjectInDatabaseException("User");
                    error.Message   = "This user is already created";
                    return(error);
                }
            }
            catch (Exception ex)
            {
                DtoOutError error = new DtoOutError();
                error.Exception = ex;
                error.Message   = ex.Message;
                return(error);
            }
        }
예제 #2
0
        public bool AssignUserCredentials(int userId, string userName, string password)
        {
            Users user = GetUserById(userId);

            if (user == null)
            {
                throw new NoEntryFoundException(userId, typeof(Users).Name);
            }

            Credentials existingCredentials = CredentialsRepository.FindByUserId(userId);

            if (existingCredentials != null)
            {
                throw new ExistingCredentialsFoundException(userId, existingCredentials.Id);
            }

            existingCredentials = CredentialsRepository.FindByUserName(userName);
            if (existingCredentials != null)
            {
                throw new ExistingCredentialsFoundException(userName);
            }


            HashedAndSaltedPassword hashAndSaltPassword =
                PasswordHelper.CryptPassword(password);

            Credentials newCredentials = new Credentials
            {
                UserId       = userId,
                UserName     = userName,
                PasswordHash = hashAndSaltPassword.PasswordHash,
                PasswordSalt = hashAndSaltPassword.PasswordSalt
            };

            CredentialsRepository.Add(newCredentials);
            return(true);
        }