Пример #1
0
        public Result Create(AdminUserDto dto)
        {
            var validator = new AdminUserValidator(_adminUserRepository);
            var result    = validator.ValidateResult(dto);

            if (!result.Success)
            {
                return(result);
            }

            var entity = Mapper.Map <AdminUser>(dto);

            entity.Salt     = SecurityHelper.GenerateSalt();
            entity.Password = SecurityHelper.GenerateHash(dto.Password, entity.Salt);

            _adminUserRepository.Insert(entity);

            foreach (var roleId in dto.Roles)
            {
                entity.Roles.Add(_adminRoleRepository.Find(roleId));
            }

            _unitOfWork.Commit();
            AdminUserCacheManager.ClearCache();

            result.Id = entity.Id;

            return(result.SetSuccess(Messages.RecordSaved));
        }
Пример #2
0
        public Result Edit(int id, AdminUserDto dto)
        {
            dto.Id = id;

            var validator = new AdminUserValidator(_adminUserRepository);
            var result    = validator.ValidateResult(dto);

            if (!result.Success)
            {
                return(result);
            }

            var entity = _adminUserRepository.AsNoTracking.Include(i => i.Roles)
                         .FirstOrDefault(s => s.Id == dto.Id);

            if (entity == null)
            {
                return(new Result().SetBlankRedirect());
            }

            Mapper.Map <AdminUser>(dto, entity);

            if (!string.IsNullOrEmpty(dto.Password))
            {
                entity.Salt     = SecurityHelper.GenerateSalt();
                entity.Password = SecurityHelper.GenerateHash(dto.Password, entity.Salt);
            }

            _adminUserRepository.Update(entity);

            ChildRoleUpdate(entity, dto);

            _unitOfWork.Commit();
            AdminUserCacheManager.ClearCache();

            return(result.SetSuccess(Messages.RecordSaved));
        }