Esempio n. 1
0
        public async Task <ActionResult> RolesEdit_Partial(EditModel model)
        {
            using (RoleDefinitionDataProvider dataProvider = new RoleDefinitionDataProvider()) {
                string         originalRole = model.OriginalName;
                RoleDefinition role         = await dataProvider.GetItemAsync(originalRole);// get the original item

                if (role == null)
                {
                    throw new Error(this.__ResStr("alreadyDeleted", "The role named \"{0}\" has been removed and can no longer be updated.", originalRole));
                }

                if (!ModelState.IsValid)
                {
                    return(PartialView(model));
                }

                role = model.GetData(role); // merge new data into original
                model.SetData(role);        // and all the data back into model for final display

                switch (await dataProvider.UpdateItemAsync(originalRole, role))
                {
                default:
                case UpdateStatusEnum.RecordDeleted:
                    throw new Error(this.__ResStr("alreadyDeleted", "The role named \"{0}\" has been removed and can no longer be updated.", originalRole));

                case UpdateStatusEnum.NewKeyExists:
                    ModelState.AddModelError(nameof(model.Name), this.__ResStr("alreadyExists", "A role named \"{0}\" already exists.", model.Name));
                    return(PartialView(model));

                case UpdateStatusEnum.OK:
                    break;
                }
                return(FormProcessed(model, this.__ResStr("okSaved", "Role \"{0}\" saved", role.Name), OnPopupClose: OnPopupCloseEnum.ReloadModule));
            }
        }
Esempio n. 2
0
        public async Task RemoveRoleFromUserAsync(int userId, string roleName)
        {
            int roleId;

            // get the role id for roleName
            using (RoleDefinitionDataProvider roleDP = new RoleDefinitionDataProvider()) {
                RoleDefinition role = await roleDP.GetItemAsync(roleName);

                if (role == null)
                {
                    throw new InternalError("Unexpected error in AddRoleToUser - expected role {0} not found", roleName);
                }
                roleId = role.RoleId;
            }
            // remove the role from the user
            using (UserDefinitionDataProvider userDP = new UserDefinitionDataProvider()) {
                UserDefinition user = await userDP.GetItemByUserIdAsync(userId);

                if (user != null)
                {
                    Role role = (from Role r in user.RolesList where r.RoleId == roleId select r).FirstOrDefault();
                    if (role != null)
                    {
                        user.RolesList.Remove(role);
                        UpdateStatusEnum status = await userDP.UpdateItemAsync(user);

                        if (status != UpdateStatusEnum.OK)
                        {
                            throw new InternalError("Unexpected status {0} updating user account in RemoveRoleFromUser", status);
                        }
                    }
                }
            }
        }
Esempio n. 3
0
        public async Task <ActionResult> RolesDisplay(string name)
        {
            using (RoleDefinitionDataProvider dataProvider = new RoleDefinitionDataProvider()) {
                RoleDefinition data = await dataProvider.GetItemAsync(name);

                if (data == null)
                {
                    throw new Error(this.__ResStr("notFound", "Role \"{0}\" not found."), name);
                }
                DisplayModel model = new DisplayModel();
                model.SetData(data);
                Module.Title = this.__ResStr("modDisplayTitle", "{0} Role", name);
                return(View(model));
            }
        }
Esempio n. 4
0
        private async Task <UserDefinition> GetSuperuserUserAsync()
        {
            using (RoleDefinitionDataProvider roleProvider = new RoleDefinitionDataProvider(SiteIdentity)) {
                RoleDefinition role = await roleProvider.GetItemAsync(Globals.Role_Superuser);

                return(new UserDefinition()
                {
                    UserName = SuperUserName,
                    RolesList = new SerializableList <Role>()
                    {
                        new Role()
                        {
                            RoleId = role.RoleId
                        }
                    },
                    UserStatus = UserStatusEnum.Approved,
                    Comment = this.__ResStr("super", "The superuser for all sites"),
                    Email = SuperUserName + "@" + Manager.CurrentSite.SiteDomain,
                    RegistrationIP = "127.0.0.1",
                });
            }
        }
Esempio n. 5
0
        public async Task AddRoleToUserAsync(int userId, string roleName)
        {
            int roleId;

            // get the role id for roleName
            using (RoleDefinitionDataProvider roleDP = new RoleDefinitionDataProvider()) {
                RoleDefinition role = await roleDP.GetItemAsync(roleName);

                if (role == null)
                {
                    throw new InternalError("Unexpected error in AddRoleToUser - expected role {0} not found", roleName);
                }
                roleId = role.RoleId;
            }
            // add the role to the user
            using (UserDefinitionDataProvider userDP = new UserDefinitionDataProvider()) {
                UserDefinition user = await userDP.GetItemByUserIdAsync(userId);

                if (user == null)
                {
                    throw new InternalError("Unexpected error in AddRoleToUser - no user found");
                }
                Role role = new Role {
                    RoleId = roleId
                };
                if (!user.RolesList.Contains(role, new RoleComparer()))
                {
                    user.RolesList.Add(role);
                    UpdateStatusEnum status = await userDP.UpdateItemAsync(user);

                    if (status != UpdateStatusEnum.OK)
                    {
                        throw new InternalError("Unexpected status {0} updating user account in AddRoleToUser", status);
                    }
                }
            }
        }
Esempio n. 6
0
 public async Task <RoleDefinition> FindByNameAsync(string roleName, CancellationToken cancellationToken)
 {
     using (RoleDefinitionDataProvider dataProvider = new RoleDefinitionDataProvider(this.CurrentSiteIdentity)) {
         return(await dataProvider.GetItemAsync(roleName));
     }
 }