示例#1
0
        /// <summary>
        /// Adds the staff handler.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="response">The response.</param>
        /// <returns></returns>
        public AddStaffProfileResponse AddStaffHandler(AddStaffProfileRequest request, AddStaffProfileResponse response)
        {
            ValidateStaffProfile(request);

            var user = CreateUser(request);

            using (var context = _entityContextFactory.CreateEntityContext())
            {
                var staffProfile = new StaffProfile
                {
                    UserId = user.Id,
                    PrimaryOrgUnitId = request.PrimaryOrgUnitId,
                    Title = request.Title
                };

                if (request.OrgUnitIds != null)
                {
                    foreach (int orgUnitId in request.OrgUnitIds)
                    {
                        var staffProfileOrgUnit = new StaffProfileOrgUnit { OrgUnitId = orgUnitId };
                        if (request.Permissions != null)
                        {
                            foreach (var permission in request.Permissions.Where(p => p.OrgUnitId == orgUnitId))
                            {
                                staffProfileOrgUnit.StaffProfileOrgUnitPermissions.Add(new StaffProfileOrgUnitPermission
                                {
                                    AppliesToDescendants = permission.AppliesToDescendants,
                                    IsGranted = permission.IsGranted,
                                    StaffPermissionValue = permission.StaffPermissionValue
                                });
                            }
                        }
                        staffProfile.StaffProfileOrgUnits.Add(staffProfileOrgUnit);
                    }
                }

                context.EntitySet<StaffProfile>().Add(staffProfile);
                context.SaveChanges();

                if (!request.IsBulkUpdateMode)
                {
                    // Update the org unit entity association data to reflect newly added staff user
                    PublishOrgUnitAssociationsHelper.PublishOrgUnitEntityAssociations(context.ObjectContext, "StaffProfiles", staffProfile.Id);
                }

                if(!request.DisableCacheRefresh)
                    UpdateStaffPermissionsCache(user.Id);

                response.StaffProfileId = staffProfile.Id;
                response.UserId = user.Id;
            }
            return response;
        }
        private UserDto CreateOrUpdateStaffProfile(string cmsUserName, string cmsRoles)
        {
            var staffProfile = GetStaffProfile(cmsUserName);
            var roleIds = GetMappedRoleIds(cmsRoles);

            if (staffProfile == null)
            {
                var request = new AddStaffProfileRequest
                {
                    UserName = cmsUserName,
                    Password = Guid.NewGuid().ToString(),
                    FirstName = cmsUserName,
                    LastName = cmsUserName,
                    EmailAddress = cmsUserName + Core.Constants.Authentication.NoReplyEmailSuffix,
                    RoleIds = roleIds
                };

                ProcessRequest<AddStaffProfileResponse>(request);
            }
            else
            {
                var request = new UpdateStaffProfileRequest
                {
                    Id = staffProfile.Id,
                    UserName = staffProfile.UserUserName,
                    FirstName = staffProfile.UserFirstName,
                    LastName = staffProfile.UserLastName,
                    EmailAddress = staffProfile.UserEmail,
                    RoleIds = roleIds
                };

                ProcessRequest<UpdateStaffProfileResponse>(request);
            }

            return GetUser(cmsUserName);
        }
示例#3
0
 private User CreateUser(AddStaffProfileRequest request)
 {
     var createUserRequest = new CreateUserRequest
     {
         UserName = request.UserName,
         Password = request.Password,
         Email = request.EmailAddress,
         NamePrefix = request.NamePrefix,
         FirstName = request.FirstName,
         MiddleName = request.MiddleName,
         LastName = request.LastName,
         NameSuffix = request.NameSuffix,
         IsApproved = true,
         IsVerified = true,
         RoleIds = FindRoles(request)
     };
     var user = _userFactory.CreateUser(createUserRequest);
     return user;
 }
示例#4
0
        private static IEnumerable<int> FindRoles(AddStaffProfileRequest request)
        {
            List<int> roles;
            if (request.RoleIds == null)
                roles = new List<int>();
            else
                roles = request.RoleIds.ToList();

            return roles.Distinct();
        }