Example #1
0
        public bool SaveRole(PortalSettings portalSettings, RoleDto roleDto, bool assignExistUsers, out KeyValuePair <HttpStatusCode, string> message)
        {
            message = new KeyValuePair <HttpStatusCode, string>();
            if (!this.IsAdmin(portalSettings) && roleDto.Id == portalSettings.AdministratorRoleId)
            {
                message = new KeyValuePair <HttpStatusCode, string>(HttpStatusCode.BadRequest, Localization.GetString("InvalidRequest", Constants.LocalResourcesFile));
                return(false);
            }
            var role = roleDto.ToRoleInfo();

            role.PortalID = portalSettings.PortalId;
            var rolename = role.RoleName.ToUpperInvariant();

            if (roleDto.Id == Null.NullInteger)
            {
                if (RoleController.Instance.GetRole(portalSettings.PortalId, r => rolename.Equals(r.RoleName, StringComparison.OrdinalIgnoreCase)) == null)
                {
                    RoleController.Instance.AddRole(role, assignExistUsers);
                    roleDto.Id = role.RoleID;
                }
                else
                {
                    message = new KeyValuePair <HttpStatusCode, string>(HttpStatusCode.BadRequest, Localization.GetString("DuplicateRole", Constants.LocalResourcesFile));
                    return(false);
                }
            }
            else
            {
                var existingRole = RoleController.Instance.GetRoleById(portalSettings.PortalId, roleDto.Id);
                if (existingRole == null)
                {
                    message = new KeyValuePair <HttpStatusCode, string>(HttpStatusCode.NotFound, Localization.GetString("RoleNotFound", Constants.LocalResourcesFile));
                    return(false);
                }

                if (existingRole.IsSystemRole)
                {
                    if (role.Description != existingRole.Description)//In System roles only description can be updated.
                    {
                        existingRole.Description = role.Description;
                        RoleController.Instance.UpdateRole(existingRole, assignExistUsers);
                    }
                }
                else if (RoleController.Instance.GetRole(portalSettings.PortalId, r => rolename.Equals(r.RoleName, StringComparison.OrdinalIgnoreCase) && r.RoleID != roleDto.Id) == null)
                {
                    existingRole.RoleName       = role.RoleName;
                    existingRole.Description    = role.Description;
                    existingRole.RoleGroupID    = role.RoleGroupID;
                    existingRole.SecurityMode   = role.SecurityMode;
                    existingRole.Status         = role.Status;
                    existingRole.IsPublic       = role.IsPublic;
                    existingRole.AutoAssignment = role.AutoAssignment;
                    existingRole.RSVPCode       = role.RSVPCode;
                    RoleController.Instance.UpdateRole(existingRole, assignExistUsers);
                }
                else
                {
                    message = new KeyValuePair <HttpStatusCode, string>(HttpStatusCode.BadRequest, Localization.GetString("DuplicateRole", Constants.LocalResourcesFile));
                    return(false);
                }
            }
            return(true);
        }
Example #2
0
        public HttpResponseMessage SaveRole(RoleDto roleDto, [FromUri] bool assignExistUsers)
        {
            try
            {
                Validate(roleDto);

                var role = roleDto.ToRoleInfo();
                role.PortalID = PortalId;
                var rolename = role.RoleName.ToUpperInvariant();

                if (roleDto.Id == Null.NullInteger)
                {
                    if (RoleController.Instance.GetRole(PortalId,
                                                        r => rolename.Equals(r.RoleName, StringComparison.InvariantCultureIgnoreCase)) == null)
                    {
                        RoleController.Instance.AddRole(role, assignExistUsers);
                        roleDto.Id = role.RoleID;
                    }
                    else
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.BadRequest,
                                                           Localization.GetString("DuplicateRole", Components.Constants.LocalResourcesFile)));
                    }
                }
                else
                {
                    var existingRole = RoleController.Instance.GetRoleById(PortalId, roleDto.Id);
                    if (existingRole == null)
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.NotFound,
                                                           Localization.GetString("RoleNotFound", Components.Constants.LocalResourcesFile)));
                    }

                    if (existingRole.IsSystemRole)
                    {
                        if (role.Description != existingRole.Description)//In System roles only description can be updated.
                        {
                            existingRole.Description = role.Description;
                            RoleController.Instance.UpdateRole(existingRole, assignExistUsers);
                        }
                    }
                    else if (RoleController.Instance.GetRole(PortalId,
                                                             r =>
                                                             rolename.Equals(r.RoleName, StringComparison.InvariantCultureIgnoreCase) &&
                                                             r.RoleID != roleDto.Id) == null)
                    {
                        RoleController.Instance.UpdateRole(role, assignExistUsers);
                    }
                    else
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.BadRequest,
                                                           Localization.GetString("DuplicateRole", Components.Constants.LocalResourcesFile)));
                    }
                }

                return(Request.CreateResponse(HttpStatusCode.OK, GetRole(roleDto.Id)));
            }
            catch (ArgumentException ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message));
            }
            catch (SecurityException ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message));
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message));
            }
        }