예제 #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 Entity.ActionStatus Manage(Entity.AddAdminUserRequest request)
        {
            Entity.ActionStatus actionStatus = new ActionStatus(true);
            try
            {
                if (request.Id == null || request.Id == Guid.Empty)
                {
                    var checkExisting = _adminUserRepository.FindBy(x => x.Email.Equals(request.Email) && x.IsActive && !x.IsDeleted).FirstOrDefault();

                    if (checkExisting == null)
                    {
                        var adminUser = Mapper.Configuration.Mapper.Map <Entity.AddAdminUserRequest, Model.AdminUser>(request);



                        adminUser.CreatedBy   = component.helper.SolutionConfiguration.CurrentUserId;
                        adminUser.CreatedDate = DateTime.Now;
                        adminUser.IsActive    = true;
                        adminUser.IsDeleted   = false;
                        adminUser.Guid        = Guid.NewGuid();
                        adminUser.CompanyGuid = component.helper.SolutionConfiguration.CompanyId;

                        actionStatus = _adminUserRepository.Insert(adminUser);

                        actionStatus.Data = Mapper.Configuration.Mapper.Map <Model.AdminUser, Entity.UserResponse>(actionStatus.Data);

                        if (!actionStatus.Success)
                        {
                            _logger.Error($"Admin User is not added in solution database, Error: {actionStatus.Message}");
                            actionStatus.Success = false;
                            actionStatus.Message = "Something Went Wrong!";
                        }
                    }
                    else
                    {
                        // _logger.Error($"AdminUser is not updated in solution database, Error: {actionStatus.Message}");
                        actionStatus.Success = false;
                        actionStatus.Message = "Email Id Already Registered.";
                    }
                }
                else
                {
                    var oldAdminUser = _adminUserRepository.FindBy(x => x.Guid.Equals(request.Id)).FirstOrDefault();
                    if (oldAdminUser == null)
                    {
                        throw new NotFoundCustomException($"{CommonException.Name.NoRecordsFound} : AdminUser");
                    }
                    else
                    {
                        var checkExisting = _adminUserRepository.FindBy(x => x.Email.Equals(request.Email) && x.IsActive && !x.IsDeleted && x.Guid != oldAdminUser.Guid).FirstOrDefault();
                        if (checkExisting == null)
                        {
                            var existingPassword = oldAdminUser.Password;
                            var dbAdminUser      = Mapper.Configuration.Mapper.Map(request, oldAdminUser);

                            dbAdminUser.CreatedBy   = oldAdminUser.CreatedBy;
                            dbAdminUser.CreatedDate = oldAdminUser.CreatedDate;
                            dbAdminUser.UpdatedDate = DateTime.Now;
                            dbAdminUser.UpdatedBy   = component.helper.SolutionConfiguration.CurrentUserId;
                            dbAdminUser.CompanyGuid = component.helper.SolutionConfiguration.CompanyId;
                            if (string.IsNullOrEmpty(dbAdminUser.Password))
                            {
                                dbAdminUser.Password = existingPassword;
                            }
                            actionStatus = _adminUserRepository.Update(dbAdminUser);

                            actionStatus.Data = Mapper.Configuration.Mapper.Map <Model.AdminUser, Entity.UserResponse>(actionStatus.Data);

                            if (!actionStatus.Success)
                            {
                                _logger.Error($"AdminUser is not updated in solution database, Error: {actionStatus.Message}");
                                actionStatus.Success = false;
                                actionStatus.Message = "Something Went Wrong!";
                            }
                        }
                        else
                        {
                            actionStatus.Success = false;
                            actionStatus.Message = "Email Id Already Registered With Another User.";
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.Error(Constants.ACTION_EXCEPTION, "AdminUserManager.Manage " + ex);
                actionStatus.Success = false;
                actionStatus.Message = ex.Message;
            }

            return(actionStatus);
        }
        public Entity.ActionStatus Manage(Entity.AddAdminUserRequest request)
        {
            Entity.ActionStatus actionStatus = new ActionStatus(true);
            try
            {
                if (request.Id == null || request.Id == Guid.Empty)
                {
                    var checkExisting = _adminUserRepository.FindBy(x => x.Email.Equals(request.Email) && x.IsActive && !x.IsDeleted).FirstOrDefault();

                    if (checkExisting == null)
                    {
                        var adminUser = Mapper.Configuration.Mapper.Map <Entity.AddAdminUserRequest, Model.AdminUser>(request);



                        adminUser.CreatedBy   = SolutionConfiguration.CurrentUserId;
                        adminUser.CreatedDate = DateTime.Now;
                        adminUser.IsActive    = true;
                        adminUser.IsDeleted   = false;
                        adminUser.Guid        = Guid.NewGuid();
                        adminUser.CompanyGuid = SolutionConfiguration.CompanyId;

                        actionStatus = _adminUserRepository.Insert(adminUser);

                        actionStatus.Data = Mapper.Configuration.Mapper.Map <Model.AdminUser, Entity.UserResponse>(actionStatus.Data);

                        if (!actionStatus.Success)
                        {
                            _logger.ErrorLog(new Exception($"Admin User is not added in solution database, Error: {actionStatus.Message}"), this.GetType().Name, MethodBase.GetCurrentMethod().Name);
                            actionStatus.Success = false;
                            actionStatus.Message = "Something Went Wrong!";
                        }
                    }
                    else
                    {
                        // _logger.Error($"AdminUser is not updated in solution database, Error: {actionStatus.Message}");
                        actionStatus.Success = false;
                        actionStatus.Message = "Email Id Already Registered.";
                    }
                }
                else
                {
                    var oldAdminUser = _adminUserRepository.FindBy(x => x.Guid.Equals(request.Id)).FirstOrDefault();
                    if (oldAdminUser == null)
                    {
                        throw new NotFoundCustomException($"{CommonException.Name.NoRecordsFound} : AdminUser");
                    }
                    else
                    {
                        var dbAdminUser = Mapper.Configuration.Mapper.Map(request, oldAdminUser);

                        dbAdminUser.CreatedBy   = oldAdminUser.CreatedBy;
                        dbAdminUser.CreatedDate = oldAdminUser.CreatedDate;
                        dbAdminUser.UpdatedDate = DateTime.Now;
                        dbAdminUser.UpdatedBy   = SolutionConfiguration.CurrentUserId;
                        dbAdminUser.CompanyGuid = SolutionConfiguration.CompanyId;

                        actionStatus = _adminUserRepository.Update(dbAdminUser);

                        actionStatus.Data = Mapper.Configuration.Mapper.Map <Model.AdminUser, Entity.UserResponse>(actionStatus.Data);

                        if (!actionStatus.Success)
                        {
                            _logger.ErrorLog(new Exception($"AdminUser is not updated in solution database, Error: {actionStatus.Message}"), this.GetType().Name, MethodBase.GetCurrentMethod().Name);
                            actionStatus.Success = false;
                            actionStatus.Message = "Something Went Wrong!";
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.ErrorLog(ex, this.GetType().Name, MethodBase.GetCurrentMethod().Name);
                actionStatus.Success = false;
                actionStatus.Message = ex.Message;
            }

            return(actionStatus);
        }