/// <summary> /// Persists a new instance of Role. Returns true on success. /// when a role is created displayname corresponds to rolename /// but rolename can never change since it is used in a cookies and coded /// into security checks in some cases /// so subsequent changes to rolename really only effect displayname /// ie for localization or customization /// to really change a rolename you can delete the role and create a new one with the desired name /// some specific required system roles (Admin, Content Administrators) /// are also not allowed to be deleted /// </summary> /// <returns></returns> public async Task <bool> SaveRole(ISiteRole role) { if (role.RoleId == -1) // new role { bool exists = await RoleExists(role.SiteId, role.DisplayName); if (exists) { log.LogError("attempt to create a duplicate role " + role.DisplayName + " for site " + role.SiteId.ToString()); return(false); } role.RoleGuid = Guid.NewGuid(); role.RoleId = dbRoles.RoleCreate( role.RoleGuid, role.SiteGuid, role.SiteId, role.DisplayName ); role.RoleName = role.DisplayName; return(role.RoleId > -1); } else { return(dbRoles.Update( role.RoleId, role.DisplayName)); } }
public static void LoadFromReader(this ISiteRole role, DbDataReader reader) { role.NormalizedRoleName = reader["RoleName"].ToString(); role.RoleName = reader["DisplayName"].ToString(); role.SiteId = new Guid(reader["SiteGuid"].ToString()); role.Id = new Guid(reader["RoleGuid"].ToString()); }
public async Task RemoveUserFromRole(ISiteUser user, ISiteRole role) { if (user == null) { throw new ArgumentNullException(nameof(user)); } if (role == null) { throw new ArgumentNullException(nameof(role)); } await _commands.RemoveUserFromRole(role.SiteId, role.Id, user.Id, CancellationToken); user.RolesChanged = true; await _commands.Update(user, CancellationToken); foreach (var handler in _userRemovedFromRoleHandlers) { try { await handler.Handle(user, role); } catch (Exception ex) { _log.LogError($"{ex.Message}-{ex.StackTrace}"); } } }
public async Task RemoveFromRoleAsync(TUser user, string role, CancellationToken cancellationToken) { if (debugLog) { log.LogInformation("RemoveFromRoleAsync"); } ThrowIfDisposed(); if (user == null) { throw new ArgumentNullException("user"); } cancellationToken.ThrowIfCancellationRequested(); int siteId = siteSettings.SiteId; if (multiTenantOptions.UseRelatedSitesMode) { siteId = multiTenantOptions.RelatedSiteId; } ISiteRole siteRole = await repo.FetchRole(siteId, role); bool result = false; if (siteRole != null) { cancellationToken.ThrowIfCancellationRequested(); result = await repo.RemoveUserFromRole(siteRole.RoleId, user.UserId); } }
public static bool IsDeletable(this ISiteRole role, List <string> rolesThatCannotBeDeleted) { if (role.RoleName == "Administrators") { return(false); } //if (role.RoleName == "Content Administrators") { return false; } //if (role.RoleName == "Authenticated Users") { return false; } //if (role.RoleName == "Role Administrators") { return false; } if (role.NormalizedRoleName == "Administrators") { return(false); } //if (role.NormalizedRoleName == "Content Administrators") { return false; } //if (role.NormalizedRoleName == "Authenticated Users") { return false; } //if (role.NormalizedRoleName == "Role Administrators") { return false; } if (rolesThatCannotBeDeleted != null) { foreach (string roleName in rolesThatCannotBeDeleted) { if (role.NormalizedRoleName == roleName) { return(false); } if (role.RoleName == roleName) { return(false); } } } return(true); }
public async Task <IList <ISiteUser> > GetUsersInRole( int siteId, string roleName) { IList <ISiteUser> users = new List <ISiteUser>(); if (multiTenantOptions.UseRelatedSitesMode) { siteId = multiTenantOptions.RelatedSiteId; } ISiteRole role = await FetchRole(siteId, roleName); int roleId = -3; if (role != null) { roleId = role.RoleId; } using (DbDataReader reader = dbRoles.GetUsersInRole(siteId, roleId, string.Empty, 1, 100000)) { while (reader.Read()) { SiteUser user = new SiteUser(); user.LoadFromReader(reader); users.Add(user); } } return(users); }
public async Task <List <int> > GetRoleIds(int siteId, string roleNamesSeparatedBySemiColons) { if (multiTenantOptions.UseRelatedSitesMode) { siteId = multiTenantOptions.RelatedSiteId; } List <int> roleIds = new List <int>(); List <string> roleNames = GetRolesNames(roleNamesSeparatedBySemiColons); foreach (string roleName in roleNames) { if (string.IsNullOrEmpty(roleName)) { continue; } ISiteRole r = await FetchRole(siteId, roleName); if (r == null) { log.LogDebug("could not get roleid for role named " + roleName); continue; } if (r.RoleId > -1) { roleIds.Add(r.RoleId); } } return(roleIds); }
/// <summary> /// Persists a new instance of Role. Returns true on success. /// when a role is created displayname corresponds to rolename /// but rolename can never change since it is used in a cookies and coded /// into security checks in some cases /// so subsequent changes to rolename really only effect displayname /// ie for localization or customization /// to really change a rolename you can delete the role and create a new one with the desired name /// some specific required system roles (Admin, Content Administrators) /// are also not allowed to be deleted /// </summary> /// <returns></returns> public async Task CreateRole( ISiteRole role, CancellationToken cancellationToken = default(CancellationToken)) { ThrowIfDisposed(); cancellationToken.ThrowIfCancellationRequested(); if (role == null) { throw new ArgumentException("role cannot be null"); } if (role.SiteId == Guid.Empty) { throw new ArgumentException("SiteId must be provided"); } if (role.Id == Guid.Empty) { throw new ArgumentException("Id must be provided"); } var siteRole = SiteRole.FromISiteRole(role); if (siteRole.NormalizedRoleName.Length == 0) { siteRole.NormalizedRoleName = siteRole.RoleName; } dbContext.Roles.Add(siteRole); int rowsAffected = await dbContext.SaveChangesAsync(cancellationToken) .ConfigureAwait(false); }
public async Task UpdateRole( ISiteRole role, CancellationToken cancellationToken = default(CancellationToken)) { ThrowIfDisposed(); cancellationToken.ThrowIfCancellationRequested(); if (role == null) { throw new ArgumentException("role cannot be null"); } if (role.SiteId == Guid.Empty) { throw new ArgumentException("SiteId must be provided"); } if (role.Id == Guid.Empty) { throw new ArgumentException("Id must be provided"); } //await EnsureProjectId().ConfigureAwait(false); var projectId = role.SiteId.ToString(); var siteRole = SiteRole.FromISiteRole(role); await roleCommands.UpdateAsync( projectId, siteRole.Id.ToString(), siteRole, cancellationToken).ConfigureAwait(false); }
public async Task<IList<ISiteUser>> GetUsersInRole( int siteId, string roleName) { IList<ISiteUser> users = new List<ISiteUser>(); ISiteRole role = await FetchRole(siteId, roleName); int roleId = -3; if(role != null) { roleId = role.RoleId; } using (DbDataReader reader = await dbRoles.GetUsersInRole(siteId, roleId, string.Empty, 1, 100000)) { while (reader.Read()) { SiteUser user = new SiteUser(); user.LoadFromReader(reader); users.Add(user); } } return users; }
public async Task UpdateRole( ISiteRole role, CancellationToken cancellationToken = default(CancellationToken)) { ThrowIfDisposed(); cancellationToken.ThrowIfCancellationRequested(); if (role == null) { throw new ArgumentException("role cannot be null"); } if (role.SiteId == Guid.Empty) { throw new ArgumentException("SiteId must be provided"); } if (role.Id == Guid.Empty) { throw new ArgumentException("Id must be provided"); } var siteRole = SiteRole.FromISiteRole(role); bool tracking = dbContext.ChangeTracker.Entries <SiteRole>().Any(x => x.Entity.Id == siteRole.Id); if (!tracking) { dbContext.Roles.Update(siteRole); } int rowsAffected = await dbContext.SaveChangesAsync(cancellationToken) .ConfigureAwait(false); }
public async Task <bool> AddUserToRole(ISiteUser user, ISiteRole role) { if (user == null) { throw new ArgumentNullException(nameof(user)); } if (role == null) { throw new ArgumentNullException(nameof(role)); } if (role.SiteId != user.SiteId) { throw new ArgumentException("user and role must have the same siteid"); } bool result = await userRepo.AddUserToRole(role.RoleId, role.RoleGuid, user.UserId, user.UserGuid); if (result) { user.RolesChanged = true; bool result2 = await userRepo.Save(user); } return(result); }
public async Task <IList <ISiteUser> > GetUsersInRole( int siteId, string roleName, CancellationToken cancellationToken = default(CancellationToken)) { cancellationToken.ThrowIfCancellationRequested(); IList <ISiteUser> users = new List <ISiteUser>(); ISiteRole role = await FetchRole(siteId, roleName); int roleId = -3; if (role != null) { roleId = role.RoleId; } using (DbDataReader reader = await dbRoles.GetUsersInRole(siteId, roleId, string.Empty, 1, 100000)) { while (reader.Read()) { SiteUser user = new SiteUser(); user.LoadFromReader(reader); users.Add(user); } } return(users); }
public async Task AddUserToRole(ISiteUser user, ISiteRole role) { if (user == null) { throw new ArgumentNullException(nameof(user)); } if (role == null) { throw new ArgumentNullException(nameof(role)); } if (role.SiteId != user.SiteId) { throw new ArgumentException("user and role must have the same siteid"); } await _commands.AddUserToRole(role.SiteId, role.Id, user.Id, CancellationToken); user.RolesChanged = true; await _commands.Update(user, CancellationToken); foreach (var handler in _userAddedToRoleHandlers) { try { await handler.Handle(user, role); } catch (Exception ex) { _log.LogError($"{ex.Message}-{ex.StackTrace}"); } } }
public async Task <bool> CreateAdminUser(ISiteSettings site) { ISiteRole adminRole = await userRepo.FetchRole(site.SiteId, "Admins", CancellationToken); if (adminRole == null) { throw new InvalidOperationException("Admins role could nto be found so cannot create admin user"); } // if using related sites mode there is a problem if we already have user [email protected] // and we create another one in the child site with the same email and login so we need to make it different // we could just skip creating this user since in related sites mode all users come from the first site // but then if the config were changed to not related sites mode there would be no admin user // so in related sites mode we create one only as a backup in case settings are changed later int countOfSites = await siteRepo.GetCount(CancellationToken); string siteDifferentiator = string.Empty; if ( (countOfSites >= 1) && (multiTenantOptions.UseRelatedSitesMode) ) { if (site.SiteId > 1) { siteDifferentiator = site.SiteId.ToInvariantString(); } } SiteUser adminUser = new SiteUser(); adminUser.SiteId = site.SiteId; adminUser.SiteGuid = site.SiteGuid; adminUser.Email = "admin" + siteDifferentiator + "@admin.com"; adminUser.NormalizedEmail = adminUser.Email; adminUser.DisplayName = "Admin"; adminUser.UserName = "******" + siteDifferentiator; adminUser.EmailConfirmed = true; adminUser.AccountApproved = true; // clear text password will be hashed upon login // this format allows migrating from mojoportal adminUser.PasswordHash = "admin||0"; //pwd/salt/format bool result = await userRepo.Save(adminUser, CancellationToken.None); result = await userRepo.AddUserToRole( adminRole.RoleId, adminRole.RoleGuid, adminUser.UserId, adminUser.UserGuid, CancellationToken.None); return(result); }
public async Task <TRole> FindByIdAsync(string roleId, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); ThrowIfDisposed(); ISiteRole role = await userRepo.FetchRole(Convert.ToInt32(roleId), cancellationToken); return((TRole)role); }
public async Task <TRole> FindByNameAsync(string normalizedRoleName, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); ThrowIfDisposed(); ISiteRole role = await userRepo.FetchRole(Site.SiteId, normalizedRoleName); return((TRole)role); }
public static void LoadFromReader(this ISiteRole role, DbDataReader reader) { role.RoleId = Convert.ToInt32(reader["RoleID"]); role.SiteId = Convert.ToInt32(reader["SiteID"]); role.RoleName = reader["RoleName"].ToString(); role.DisplayName = reader["DisplayName"].ToString(); role.SiteGuid = new Guid(reader["SiteGuid"].ToString()); role.RoleGuid = new Guid(reader["RoleGuid"].ToString()); }
public static RoleViewModel FromISiteRole(ISiteRole siteRole) { RoleViewModel model = new RoleViewModel(); model.Id = siteRole.Id; model.NormalizedRoleName = siteRole.NormalizedRoleName; model.RoleName = siteRole.RoleName; model.SiteId = siteRole.SiteId; return model; }
public static RoleViewModel FromISiteRole(ISiteRole siteRole) { RoleViewModel model = new RoleViewModel(); model.Id = siteRole.Id; model.NormalizedRoleName = siteRole.NormalizedRoleName; model.RoleName = siteRole.RoleName; model.SiteId = siteRole.SiteId; return(model); }
//[MvcSiteMapNode(Title = "New Role", ParentKey = "Roles", Key = "RoleEdit")] public async Task <IActionResult> RoleEdit( Guid?siteGuid, int?roleId) { ISiteSettings selectedSite; // only server admin site can edit other sites settings if ((siteGuid.HasValue) && (siteGuid.Value != Guid.Empty) && (siteGuid.Value != siteManager.CurrentSite.SiteGuid) && (siteManager.CurrentSite.IsServerAdminSite)) { selectedSite = await siteManager.Fetch(siteGuid.Value); ViewData["Title"] = string.Format(CultureInfo.CurrentUICulture, "{0} - New Role", selectedSite.SiteName); } else { selectedSite = siteManager.CurrentSite; ViewBag.Title = "New Role"; } RoleViewModel model = new RoleViewModel(); model.SiteGuid = selectedSite.SiteGuid; model.SiteId = selectedSite.SiteId; model.Heading = "New Role"; if (roleId.HasValue) { ISiteRole role = await RoleManager.FetchRole(roleId.Value); if ((role != null) && (role.SiteId == selectedSite.SiteId)) { model = RoleViewModel.FromISiteRole(role); ViewBag.Title = "Edit Role"; model.Heading = "Edit Role"; // below we are just manipiulating the bread crumbs //var node = SiteMaps.Current.FindSiteMapNodeFromKey("RoleEdit"); //var node = SiteMaps.Current.FindSiteMapNodeFromKey("RoleEdit"); //if (node != null) //{ // node.Title = "Edit Role"; //} } } return(View(model)); }
public static RoleViewModel FromISiteRole(ISiteRole siteRole) { RoleViewModel model = new RoleViewModel(); model.RoleGuid = siteRole.RoleGuid; model.RoleId = siteRole.RoleId; model.RoleName = siteRole.RoleName; model.DisplayName = siteRole.DisplayName; model.SiteGuid = siteRole.SiteGuid; model.SiteId = siteRole.SiteId; return model; }
public static RoleViewModel FromISiteRole(ISiteRole siteRole) { RoleViewModel model = new RoleViewModel(); model.RoleGuid = siteRole.RoleGuid; model.RoleId = siteRole.RoleId; model.RoleName = siteRole.RoleName; model.DisplayName = siteRole.DisplayName; model.SiteGuid = siteRole.SiteGuid; model.SiteId = siteRole.SiteId; return(model); }
public static SiteRole FromISiteRole(ISiteRole i) { SiteRole r = new SiteRole(); r.RoleName = i.RoleName; r.MemberCount = i.MemberCount; r.Id = i.Id; r.NormalizedRoleName = i.NormalizedRoleName; r.SiteId = i.SiteId; // r.ConcurrencyStamp = i.ConcurrencyStamp; return r; }
public static SiteRole FromISiteRole(ISiteRole i) { SiteRole r = new SiteRole(); r.DisplayName = i.DisplayName; r.MemberCount = i.MemberCount; r.RoleGuid = i.RoleGuid; r.RoleId = i.RoleId; r.RoleName = i.RoleName; r.SiteGuid = i.SiteGuid; r.SiteId = i.SiteId; return r; }
public static SiteRole FromISiteRole(ISiteRole i) { SiteRole r = new SiteRole(); r.RoleName = i.RoleName; r.MemberCount = i.MemberCount; r.Id = i.Id; r.NormalizedRoleName = i.NormalizedRoleName; r.SiteId = i.SiteId; // r.ConcurrencyStamp = i.ConcurrencyStamp; return(r); }
//public async Task<int> GetNewestUserId(int siteId) //{ // return await dbSiteUser.GetNewestUserId(siteId); //} #endregion #region Roles /// <summary> /// Persists a new instance of Role. Returns true on success. /// when a role is created displayname corresponds to rolename /// but rolename can never change since it is used in a cookies and coded /// into security checks in some cases /// so subsequent changes to rolename really only effect displayname /// ie for localization or customization /// to really change a rolename you can delete the role and create a new one with the desired name /// some specific required system roles (Admin, Content Administrators) /// are also not allowed to be deleted /// </summary> /// <returns></returns> public async Task <bool> SaveRole( ISiteRole role, CancellationToken cancellationToken = default(CancellationToken)) { if (role.SiteId == -1) { throw new ArgumentException("SiteId must be provided"); } if (role.SiteGuid == Guid.Empty) { throw new ArgumentException("SiteGuid must be provided"); } cancellationToken.ThrowIfCancellationRequested(); if (role.RoleId == -1) // new role { bool exists = await RoleExists(role.SiteId, role.DisplayName); if (exists) { log.LogError("attempt to create a duplicate role " + role.DisplayName + " for site " + role.SiteId.ToString()); return(false); } role.RoleGuid = Guid.NewGuid(); role.RoleId = await dbRoles.RoleCreate( role.RoleGuid, role.SiteGuid, role.SiteId, role.DisplayName, cancellationToken ); role.RoleName = role.DisplayName; return(role.RoleId > -1); } else { return(await dbRoles.Update( role.RoleId, role.DisplayName, cancellationToken)); } }
public static SiteRole FromISiteRole(ISiteRole i) { SiteRole r = new SiteRole(); r.DisplayName = i.DisplayName; r.MemberCount = i.MemberCount; r.RoleGuid = i.RoleGuid; r.RoleId = i.RoleId; r.RoleName = i.RoleName; r.SiteGuid = i.SiteGuid; r.SiteId = i.SiteId; return(r); }
public static SiteRole FromISiteRole(ISiteRole i) { SiteRole r = new SiteRole { RoleName = i.RoleName, MemberCount = i.MemberCount, Id = i.Id, NormalizedRoleName = i.NormalizedRoleName, SiteId = i.SiteId }; // r.ConcurrencyStamp = i.ConcurrencyStamp; return(r); }
public async Task RemoveUserFromRole(ISiteUser user, ISiteRole role) { if (user == null) { throw new ArgumentNullException(nameof(user)); } if (role == null) { throw new ArgumentNullException(nameof(role)); } await commands.RemoveUserFromRole(role.Id, user.Id, CancellationToken); user.RolesChanged = true; await commands.Update(user, CancellationToken); }
public async Task <TRole> FindByNameAsync(string normalizedRoleName, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); ThrowIfDisposed(); int siteId = Site.SiteId; if (multiTenantOptions.UseRelatedSitesMode) { siteId = multiTenantOptions.RelatedSiteId; } ISiteRole role = await userRepo.FetchRole(siteId, normalizedRoleName, cancellationToken); return((TRole)role); }
public async Task AddUserToRole(ISiteUser user, ISiteRole role) { if (user == null) { throw new ArgumentNullException(nameof(user)); } if (role == null) { throw new ArgumentNullException(nameof(role)); } if (role.SiteId != user.SiteId) { throw new ArgumentException("user and role must have the same siteid"); } await commands.AddUserToRole(role.Id, user.Id, CancellationToken); user.RolesChanged = true; await commands.Update(user, CancellationToken); }
public async Task <bool> RemoveUserFromRole(ISiteUser user, ISiteRole role) { if (user == null) { throw new ArgumentNullException(nameof(user)); } if (role == null) { throw new ArgumentNullException(nameof(role)); } bool result = await userRepo.RemoveUserFromRole(role.RoleId, user.UserId); if (result) { user.RolesChanged = true; bool result2 = await userRepo.Save(user); } return(result); }
public async Task <IActionResult> RemoveUser( Guid?siteGuid, int roleId, int userId) { ISiteSettings selectedSite; // only server admin site can edit other sites settings if ((siteGuid.HasValue) && (siteGuid.Value != Guid.Empty) && (siteGuid.Value != siteManager.CurrentSite.SiteGuid) && (siteManager.CurrentSite.IsServerAdminSite)) { selectedSite = await siteManager.Fetch(siteGuid.Value); } else { selectedSite = siteManager.CurrentSite; } ISiteUser user = await UserManager.Fetch(selectedSite.SiteId, userId); if (user != null) { ISiteRole role = await RoleManager.FetchRole(roleId); if ((role != null) && (role.SiteId == selectedSite.SiteId)) { bool result = await RoleManager.RemoveUserFromRole(user, role); if (result) { this.AlertWarning(string.Format( "<b>{0}</b> was successfully removed from the role {1}.", user.DisplayName, role.DisplayName) , true); } } } return(RedirectToAction("RoleMembers", new { siteGuid = selectedSite.SiteGuid, roleId = roleId })); }
//public async Task<int> GetNewestUserId(int siteId) //{ // return await dbSiteUser.GetNewestUserId(siteId); //} #endregion #region Roles /// <summary> /// Persists a new instance of Role. Returns true on success. /// when a role is created displayname corresponds to rolename /// but rolename can never change since it is used in a cookies and coded /// into security checks in some cases /// so subsequent changes to rolename really only effect displayname /// ie for localization or customization /// to really change a rolename you can delete the role and create a new one with the desired name /// some specific required system roles (Admin, Content Administrators) /// are also not allowed to be deleted /// </summary> /// <returns></returns> public async Task<bool> SaveRole(ISiteRole role, CancellationToken cancellationToken = default(CancellationToken)) { cancellationToken.ThrowIfCancellationRequested(); if (role.RoleId == -1) // new role { bool exists = await RoleExists(role.SiteId, role.DisplayName, cancellationToken); if (exists) { log.LogError("attempt to create a duplicate role " + role.DisplayName + " for site " + role.SiteId.ToString()); return false; } role.RoleGuid = Guid.NewGuid(); role.RoleId = await dbRoles.RoleCreate( role.RoleGuid, role.SiteGuid, role.SiteId, role.DisplayName, cancellationToken ); role.RoleName = role.DisplayName; return (role.RoleId > -1); } else { return await dbRoles.Update( role.RoleId, role.DisplayName, cancellationToken); } }
//public async Task<int> GetNewestUserId(int siteId) //{ // return await dbSiteUser.GetNewestUserId(siteId); //} #endregion #region Roles /// <summary> /// Persists a new instance of Role. Returns true on success. /// when a role is created displayname corresponds to rolename /// but rolename can never change since it is used in a cookies and coded /// into security checks in some cases /// so subsequent changes to rolename really only effect displayname /// ie for localization or customization /// to really change a rolename you can delete the role and create a new one with the desired name /// some specific required system roles (Admin, Content Administrators) /// are also not allowed to be deleted /// </summary> /// <returns></returns> public async Task<bool> SaveRole(ISiteRole role) { if (role.SiteId == -1) { throw new ArgumentException("SiteId must be provided"); } if (role.SiteGuid == Guid.Empty) { throw new ArgumentException("SiteGuid must be provided"); } if (role.RoleId == -1) // new role { bool exists = await RoleExists(role.SiteId, role.DisplayName); if (exists) { log.LogError("attempt to create a duplicate role " + role.DisplayName + " for site " + role.SiteId.ToString()); return false; } role.RoleGuid = Guid.NewGuid(); role.RoleId = await dbRoles.RoleCreate( role.RoleGuid, role.SiteGuid, role.SiteId, role.DisplayName ); role.RoleName = role.DisplayName; return (role.RoleId > -1); } else { return await dbRoles.Update( role.RoleId, role.DisplayName); } }
public async Task<bool> SaveRole(ISiteRole role) { return await implementation.SaveRole(role); }
public async Task<bool> SaveRole(ISiteRole role, CancellationToken cancellationToken = default(CancellationToken)) { return await implementation.SaveRole(role, cancellationToken); }
/// <summary> /// Persists a new instance of Role. Returns true on success. /// when a role is created displayname corresponds to rolename /// but rolename can never change since it is used in a cookies and coded /// into security checks in some cases /// so subsequent changes to rolename really only effect displayname /// ie for localization or customization /// to really change a rolename you can delete the role and create a new one with the desired name /// some specific required system roles (Admin, Content Administrators) /// are also not allowed to be deleted /// </summary> /// <returns></returns> public async Task<bool> SaveRole(ISiteRole role) { if (role.RoleId == -1) // new role { bool exists = await RoleExists(role.SiteId, role.DisplayName); if (exists) { log.LogError("attempt to create a duplicate role " + role.DisplayName + " for site " + role.SiteId.ToString()); return false; } role.RoleGuid = Guid.NewGuid(); role.RoleId = dbRoles.RoleCreate( role.RoleGuid, role.SiteGuid, role.SiteId, role.DisplayName ); role.RoleName = role.DisplayName; return (role.RoleId > -1); } else { return dbRoles.Update( role.RoleId, role.DisplayName); } }
public async Task UpdateRole( ISiteRole role, CancellationToken cancellationToken = default(CancellationToken)) { ThrowIfDisposed(); cancellationToken.ThrowIfCancellationRequested(); if (role == null) { throw new ArgumentException("role cannot be null"); } if (role.SiteId == Guid.Empty) { throw new ArgumentException("SiteId must be provided"); } if (role.Id == Guid.Empty) { throw new ArgumentException("Id must be provided"); } //await EnsureProjectId().ConfigureAwait(false); var projectId = role.SiteId.ToString(); var siteRole = SiteRole.FromISiteRole(role); await roleCommands.UpdateAsync( projectId, siteRole.Id.ToString(), siteRole, cancellationToken).ConfigureAwait(false); }
public async Task UpdateRole( ISiteRole role, CancellationToken cancellationToken = default(CancellationToken)) { ThrowIfDisposed(); cancellationToken.ThrowIfCancellationRequested(); if (role == null) { throw new ArgumentException("role cannot be null"); } if (role.SiteId == Guid.Empty) { throw new ArgumentException("SiteId must be provided"); } if (role.Id == Guid.Empty) { throw new ArgumentException("Id must be provided"); } var siteRole = SiteRole.FromISiteRole(role); bool tracking = dbContext.ChangeTracker.Entries<SiteRole>().Any(x => x.Entity.Id == siteRole.Id); if (!tracking) { dbContext.Roles.Update(siteRole); } int rowsAffected = await dbContext.SaveChangesAsync(cancellationToken) .ConfigureAwait(false); }
/// <summary> /// Persists a new instance of Role. Returns true on success. /// when a role is created displayname corresponds to rolename /// but rolename can never change since it is used in a cookies and coded /// into security checks in some cases /// so subsequent changes to rolename really only effect displayname /// ie for localization or customization /// to really change a rolename you can delete the role and create a new one with the desired name /// some specific required system roles (Admin, Content Administrators) /// are also not allowed to be deleted /// </summary> /// <returns></returns> public async Task CreateRole( ISiteRole role, CancellationToken cancellationToken = default(CancellationToken)) { ThrowIfDisposed(); cancellationToken.ThrowIfCancellationRequested(); if (role == null) { throw new ArgumentException("role cannot be null"); } if (role.SiteId == Guid.Empty) { throw new ArgumentException("SiteId must be provided"); } if (role.Id == Guid.Empty) { throw new ArgumentException("Id must be provided"); } var siteRole = SiteRole.FromISiteRole(role); if (siteRole.NormalizedRoleName.Length == 0) { siteRole.NormalizedRoleName = siteRole.RoleName; } dbContext.Roles.Add(siteRole); int rowsAffected = await dbContext.SaveChangesAsync(cancellationToken) .ConfigureAwait(false); }
/// <summary> /// Persists a new instance of Role. Returns true on success. /// when a role is created displayname corresponds to rolename /// but rolename can never change since it is used in a cookies and coded /// into security checks in some cases /// so subsequent changes to rolename really only effect displayname /// ie for localization or customization /// to really change a rolename you can delete the role and create a new one with the desired name /// some specific required system roles (Admin, Content Administrators) /// are also not allowed to be deleted /// </summary> /// <returns></returns> public async Task<bool> SaveRole(ISiteRole role, CancellationToken cancellationToken = default(CancellationToken)) { if (role == null) { return false; } if (role.SiteId == -1) { throw new ArgumentException("SiteId must be provided"); } if (role.SiteGuid == Guid.Empty) { throw new ArgumentException("SiteGuid must be provided"); } SiteRole siteRole = SiteRole.FromISiteRole(role); if(siteRole.RoleId == -1) { siteRole.RoleId = 0; if(siteRole.RoleName.Length == 0) { siteRole.RoleName = siteRole.DisplayName; } dbContext.Roles.Add(siteRole); } else { bool tracking = dbContext.ChangeTracker.Entries<SiteRole>().Any(x => x.Entity.RoleId == siteRole.RoleId); if (!tracking) { dbContext.Roles.Update(siteRole); } } int rowsAffected = await dbContext.SaveChangesAsync(cancellationToken); if(role.RoleId == -1) { //update the original with the new keys on insert role.RoleId = siteRole.RoleId; role.RoleGuid = siteRole.RoleGuid; } return rowsAffected > 0; }