Esempio n. 1
0
        public UserModel GetById(int id)
        {
            var userEntity = _repo.GetUser(id);

            if (userEntity == null)
            {
                return(null);
            }

            return(MapToModel(userEntity));
        }
Esempio n. 2
0
        public SellerModel GetSellerByUserId(int userId)
        {
            var sellerEntity = _repo.GetSeller(userId);

            if (sellerEntity == null)
            {
                var userEntity = _repo.GetUser(userId);
                return(MapUserSeller(userEntity));
            }
            else
            {
                return(MapGetSeller(sellerEntity));
            }
        }
Esempio n. 3
0
        //update user
        public void Update(User userParam, string oldPassword, string newPassword = null)
        {
            //var user = _context.Users.Find(userParam.Id);
            var user = _repo.GetUser(userParam.Id);

            if (user == null)
            {
                throw new AppException("User not found");
            }

            // update username if it has changed
            if (!string.IsNullOrWhiteSpace(userParam.Username) && userParam.Username != user.Username)
            {
                // throw error if the new username is already taken
                if (_repo.GetUsers().Any(x => x.Username == userParam.Username))
                {
                    throw new AppException("Username " + userParam.Username + " is already taken");
                }

                user.Username = userParam.Username;
            }

            // update user properties if provided
            if (!string.IsNullOrWhiteSpace(userParam.FirstName))
            {
                user.FirstName = userParam.FirstName;
            }

            if (!string.IsNullOrWhiteSpace(userParam.LastName))
            {
                user.LastName = userParam.LastName;
            }

            if (!string.IsNullOrWhiteSpace(userParam.Username))
            {
                user.Username = userParam.Username;
            }

            if (!string.IsNullOrWhiteSpace(userParam.Role))
            {
                user.Role = userParam.Role;
            }

            // update password if provided
            if (!string.IsNullOrWhiteSpace(newPassword))
            {
                if (!VerifyPasswordHash(oldPassword, user.PasswordHash, user.PasswordSalt))
                {
                    byte[] passwordHash, passwordSalt;
                    CreatePasswordHash(newPassword, out passwordHash, out passwordSalt);

                    user.PasswordHash = passwordHash;
                    user.PasswordSalt = passwordSalt;
                }
                else
                {
                    throw new AppException("Password already exist,try a new password");
                }
            }

            _repo.UpdateUser(user);
        }