Beispiel #1
0
        public void SetRoleAndSite(SetUserRoleAndSiteRequest request)
        {
            _log.Add(request);
            
            //todo: throw errors if things are not set.
            var userToUpdate = _userRepository.GetByIdAndCompanyIdIncludeDeleted(request.UserToUpdateId, request.CompanyId);
            _userRepository.Initialize(userToUpdate.Site);
            _userRepository.Initialize(userToUpdate.Role); //Need to do this or you will get the 'collection not processed by Flush()' auditing error!
            var actioningUser = _auditedUserRepository.GetByIdAndCompanyId(request.ActioningUserId, request.CompanyId);
            SiteStructureElement site = null;

            if (request.PermissionsApplyToAllSites)
            {
                site = GetMainSite(request.CompanyId);
                _siteStructureElementRepository.Initialize(site);
            }
            else if (request.SiteId != default(long))
            {
                site = _siteStructureElementRepository.LoadById(request.SiteId);
                _siteStructureElementRepository.Initialize(site);
            }

            Role role = null;

            if (request.RoleId != default(Guid))
            {
                role = _roleRepository.LoadById(request.RoleId);
                _roleRepository.Initialize(role);
            }

            userToUpdate.SetRoleAndSite(role, site, actioningUser);
            _userRepository.Save(userToUpdate);
            _userRepository.Flush();
        }
        private bool CreateUpdateReinstateUser(EmployeeViewModel employeeViewModel)
        {        
            if ( (!employeeViewModel.UserRoleId.HasValue  || employeeViewModel.UserRoleId.Value == Guid.Empty ) 
                || (!employeeViewModel.UserSiteId.HasValue || employeeViewModel.UserSiteId.Value == 0 )
                && (!employeeViewModel.UserSiteGroupId.HasValue || employeeViewModel.UserSiteGroupId.Value == 0)
                && (!employeeViewModel.UserPermissionsApplyToAllSites)
                )
            {
                // assume we aren't trying to create a userCreateUser
                return true;
            }

            EmployeeDto employee = _employeeService.GetEmployee(employeeViewModel.EmployeeId.Value, employeeViewModel.CompanyId);

            if (employee.User == null)
            {
                //CREATING NEW USER
                var registerEmployeeAsUserRequest = AddUserForEmployeeMapper.Map(employeeViewModel, CurrentUser.UserId);
                
                if (!RegistrationAttemptIsValid(employee, registerEmployeeAsUserRequest))
                    return false;

                RegisterEmployeeAsUser(employee, registerEmployeeAsUserRequest);
            } else if (employee.User.Deleted)
            {
                // REINSTATE USER
                var user = _userService.GetByIdAndCompanyIdIncludeDeleted(employee.User.Id, employee.User.CompanyId);

                if (_newRegistrationRequestService.HasEmailBeenRegistered(user.Employee.MainContactDetails.Email))
                {
                    ModelState.AddModelError("User", "Sorry you are unable to reinstate this user: the email address has been registered to another user");
                    return true;
                }
                
                _userService.ReinstateUser(employee.User.Id, CurrentUser.CompanyId, CurrentUser.UserId);
            }
            else
            {
                //UPDATING USER
                //set role and site
                var roleSiteRequest = new SetUserRoleAndSiteRequest();
                roleSiteRequest.ActioningUserId = CurrentUser.UserId;
                roleSiteRequest.CompanyId = CurrentUser.CompanyId;
                roleSiteRequest.RoleId = employeeViewModel.UserRoleId.Value;
                roleSiteRequest.SiteId = employeeViewModel.UserSiteId ?? employeeViewModel.UserSiteGroupId ?? 0;
                roleSiteRequest.UserToUpdateId = employee.User != null ? employee.User.Id : new Guid();
                roleSiteRequest.PermissionsApplyToAllSites = employeeViewModel.UserPermissionsApplyToAllSites;
               
                _userService.SetRoleAndSite(roleSiteRequest);
            }
            
            return true;
        }