/// <summary> /// Load the static Authorizations object /// </summary> public static void Load() { Authorizations = new Dictionary<string, Dictionary<int, Dictionary<string, List<AuthRule>>>>(); AuthService authService = new AuthService(); foreach ( Auth auth in authService.Queryable(). OrderBy( A => A.EntityType ).ThenBy( A => A.EntityId ).ThenBy( A => A.Action ).ThenBy( A => A.Order ) ) { if ( !Authorizations.ContainsKey( auth.EntityType ) ) Authorizations.Add( auth.EntityType, new Dictionary<int, Dictionary<string, List<AuthRule>>>() ); Dictionary<int, Dictionary<string, List<AuthRule>>> entityAuths = Authorizations[auth.EntityType]; if ( !entityAuths.ContainsKey( auth.EntityId ?? 0 ) ) entityAuths.Add( auth.EntityId ?? 0, new Dictionary<string, List<AuthRule>>() ); Dictionary<string, List<AuthRule>> instanceAuths = entityAuths[auth.EntityId ?? 0]; if ( !instanceAuths.ContainsKey( auth.Action ) ) instanceAuths.Add( auth.Action, new List<AuthRule>() ); List<AuthRule> actionPermissions = instanceAuths[auth.Action]; actionPermissions.Add( new AuthRule( auth.Id, auth.AllowOrDeny, auth.SpecialRole, auth.PersonId, auth.GroupId, auth.Order) ); } }
/// <summary> /// Reloads the authorizations for the specified entity and action. /// </summary> /// <param name="entityType">Type of the entity.</param> /// <param name="entityId">The entity id.</param> /// <param name="action">The action.</param> public static void ReloadAction( string entityType, int entityId, string action ) { // If there's no Authorizations object, create it if ( Authorizations == null ) Load(); else { // Delete the current authorizations if ( Authorizations.ContainsKey( entityType ) ) if ( Authorizations[entityType].ContainsKey( entityId ) ) if ( Authorizations[entityType][entityId].ContainsKey( action ) ) Authorizations[entityType][entityId][action] = new List<AuthRule>(); // Find the Authrules for the given entity type, entity id, and action AuthService authService = new AuthService(); foreach ( Auth auth in authService.GetAuths(entityType, entityId, action)) { if ( !Authorizations.ContainsKey( auth.EntityType ) ) Authorizations.Add( auth.EntityType, new Dictionary<int, Dictionary<string, List<AuthRule>>>() ); Dictionary<int, Dictionary<string, List<AuthRule>>> entityAuths = Authorizations[auth.EntityType]; if ( !entityAuths.ContainsKey( auth.EntityId ?? 0 ) ) entityAuths.Add( auth.EntityId ?? 0, new Dictionary<string, List<AuthRule>>() ); Dictionary<string, List<AuthRule>> instanceAuths = entityAuths[auth.EntityId ?? 0]; if ( !instanceAuths.ContainsKey( auth.Action ) ) instanceAuths.Add( auth.Action, new List<AuthRule>() ); List<AuthRule> actionPermissions = instanceAuths[auth.Action]; actionPermissions.Add( new AuthRule( auth.Id, auth.AllowOrDeny, auth.SpecialRole, auth.PersonId, auth.GroupId, auth.Order ) ); } } }
/// <summary> /// Copies the authorizations from one <see cref="ISecured"/> object to another /// </summary> /// <param name="sourceEntity">The source entity.</param> /// <param name="targetEntity">The target entity.</param> /// <param name="personId">The person id.</param> public static void CopyAuthorization( ISecured sourceEntity, ISecured targetEntity, int? personId ) { using ( new Rock.Data.UnitOfWorkScope() ) { // If there's no Authorizations object, create it if ( Authorizations == null ) Load(); AuthService authService = new AuthService(); // Delete the current authorizations for the target entity foreach ( Auth auth in authService.GetByEntityTypeAndEntityId( targetEntity.AuthEntity, targetEntity.Id ) ) authService.Delete( auth, personId ); Dictionary<string, List<AuthRule>> newActions = new Dictionary<string, List<AuthRule>>(); int order = 0; if ( Authorizations.ContainsKey( sourceEntity.AuthEntity ) && Authorizations[sourceEntity.AuthEntity].ContainsKey( sourceEntity.Id ) ) foreach ( KeyValuePair<string, List<AuthRule>> action in Authorizations[sourceEntity.AuthEntity][sourceEntity.Id] ) if ( targetEntity.SupportedActions.Contains( action.Key ) ) { newActions.Add( action.Key, new List<AuthRule>() ); foreach ( AuthRule rule in action.Value ) { Auth auth = new Auth(); auth.EntityType = targetEntity.AuthEntity; auth.EntityId = targetEntity.Id; auth.Order = order; auth.Action = action.Key; auth.AllowOrDeny = rule.AllowOrDeny; auth.SpecialRole = rule.SpecialRole; auth.PersonId = rule.PersonId; auth.GroupId = rule.GroupId; authService.Add( auth, personId ); authService.Save( auth, personId ); newActions[action.Key].Add( new AuthRule( rule.Id, rule.AllowOrDeny, rule.SpecialRole, rule.PersonId, rule.GroupId, rule.Order ) ); order++; } } if ( !Authorizations.ContainsKey( targetEntity.AuthEntity ) ) Authorizations.Add( targetEntity.AuthEntity, new Dictionary<int, Dictionary<string, List<AuthRule>>>() ); Dictionary<int, Dictionary<string, List<AuthRule>>> entityType = Authorizations[targetEntity.AuthEntity]; if ( !entityType.ContainsKey( targetEntity.Id ) ) entityType.Add( targetEntity.Id, new Dictionary<string, List<AuthRule>>() ); entityType[targetEntity.Id] = newActions; } }