public ActionResult Create(int id, string name, int[] rightIds) { Role role = new Role(); role.Site = Context.ManagedSite; try { UpdateModel(role, new[] { "Name" }); if (rightIds != null && rightIds.Length > 0) { IList<Right> rights = userService.GetRightsByIds(rightIds); foreach (Right right in rights) { log.DebugFormat("RightId {0}", right.Id); role.Rights.Add(right); } } userService.UpdateRole(role); // Show the confirmation message MessageModel message = new MessageModel { Text = "Role created successfully!", Icon = MessageModel.MessageIcon.Info, CssClass = "margin-topbottom", IsClosable = true }; RegisterMessage(message, true); return RedirectToAction("Index"); } catch (Exception ex) { log.Error("RolesController.Create", ex); MessageModel message = new MessageModel { Text = GlobalResource("Message_GenericError"), Icon = MessageModel.MessageIcon.Alert, CssClass = "margin-topbottom" }; RegisterMessage(message); } RoleModel model = new RoleModel() { Role = userService.GetRoleById(id), AllRights = userService.GetAllRights() }; return View("NewRole", model); }
/// <summary> /// Create the default roles /// </summary> /// <param name="site"></param> public void CreateDefaultRoles(Site site) { log.Debug("DefaultUserService.CreateDefaultRoles: Start"); string[] administratorRoleRights = new string[]{Rights.AdminAccess, Rights.SiteCreate, Rights.SiteDelete, Rights.DashboardAccess , Rights.PostsView, Rights.PostsEdit, Rights.PostsDelete, Rights.CommentsView, Rights.CommentsEdit, Rights.CommentsDelete, Rights.PagesView, Rights.PagesEdit, Rights.PagesDelete, Rights.SiteSettingsView, Rights.SiteSettingsEdit, Rights.TemplatesView, Rights.TemplatesChange , Rights.UsersView, Rights.UsersEdit, Rights.UsersDelete, Rights.RolesView, Rights.RolesEdit, Rights.RolesDelete, Rights.FilesView, Rights.FilesEdit, Rights.FilesUpload, Rights.FilesDelete, Rights.SystemConfigurationView, Rights.SystemConfigurationEdit }; string[] editorsRoleRights = new string[]{ Rights.AdminAccess, Rights.DashboardAccess , Rights.PostsView, Rights.PostsEdit, Rights.CommentsView, Rights.CommentsEdit, Rights.CommentsDelete, Rights.PagesView, Rights.SiteSettingsView, Rights.TemplatesView, Rights.UsersView, Rights.RolesView, Rights.FilesView, Rights.FilesEdit, Rights.FilesUpload, Rights.FilesDelete }; string[] authenticatedUsersRoleRights = new string[]{ Rights.AdminAccess, Rights.DashboardAccess }; string[] demoUsersRoleRights = new string[]{ Rights.AdminAccess, Rights.DashboardAccess , Rights.PostsView, Rights.CommentsView, Rights.PagesView, Rights.SiteSettingsView, Rights.TemplatesView, Rights.UsersView, Rights.RolesView, Rights.FilesView, }; const string administratorsRoleLabel = "Administrators"; const string editorsRoleLabel = "Editors"; const string authenticatedUsersLabel = "Authenticated Users"; const string demoUsersRoleLabel = "Demo Users"; IList<Right> rights = GetAllRights(); // Create Admnistrators Role Role administratorsRole = new Role { Name = administratorsRoleLabel, Site = site }; // Add the configured rights administratorRoleRights.Each().Do((rightName) => { Right right = (from r in rights where r.Name == rightName select r).Single(); administratorsRole.Rights.Add(right); } ); // Create Editors Role Role editorsRole = new Role { Name = editorsRoleLabel, Site = site }; // Add the configured rights editorsRoleRights.Each().Do((rightName) => { Right right = (from r in rights where r.Name == rightName select r).Single(); editorsRole.Rights.Add(right); } ); // Create Authenticated Users Role Role authenticatedUsersRole = new Role { Name = authenticatedUsersLabel, Site = site }; // Add the configured rights authenticatedUsersRoleRights.Each().Do((rightName) => { Right right = (from r in rights where r.Name == rightName select r).Single(); authenticatedUsersRole.Rights.Add(right); } ); // Create Authenticated Users Role Role demoRole = new Role { Name = demoUsersRoleLabel, Site = site }; // Add the configured rights demoUsersRoleRights.Each().Do((rightName) => { Right right = (from r in rights where r.Name == rightName select r).Single(); demoRole.Rights.Add(right); } ); //using (NHTransactionScope tx = new NHTransactionScope()) //{ UpdateRole(administratorsRole); UpdateRole(editorsRole); UpdateRole(authenticatedUsersRole); UpdateRole(demoRole); //tx.VoteCommit(); log.Debug("DefaultUserService.CreateDefaultRoles: Default Roles created"); //} log.Debug("DefaultUserService.CreateDefaultRoles: End"); }
// TODO: rename to Save public void UpdateRole(Role role) { //using (NHTransactionScope tx = new NHTransactionScope()) //{ Repository<Role>.Save(role); // tx.VoteCommit(); //} }
public Paginator<User> GetUsersInRolePaginator(Role role, int pageSize) { IDetachedQuery query = new DetachedQuery("from User u where :role in elements(u.Roles) and u.Site = :site and u.IsLogicallyDeleted = 0"); query.SetEntity("role", role); query.SetEntity("site", role.Site); return Repository<User>.GetPaginator(query, pageSize); }
public IList<User> GetUsersInRole(Role role) { IDetachedQuery dq = new DetachedQuery("from User u where :role in elements(u.Roles) and u.Site = :site and u.IsLogicallyDeleted = 0"); dq.SetEntity("role", role); dq.SetEntity("site", role.Site); using (ISession session = Session){ IQuery query = dq.GetExecutableQuery(session); return query.List<User>(); } }
/// <summary> /// Determine if the user is in a give Role. /// </summary> /// <param name="user"></param> /// <param name="roleToCheck"></param> /// <returns></returns> public static bool IsInRole(this User user, Role roleToCheck) { foreach (Role role in user.Roles) { if (role.RoleId == roleToCheck.RoleId && role.Name == roleToCheck.Name) { return true; } } return false; }