/// <summary> /// Handles a single added permissable entity by adding it as a cam resource. /// </summary> /// <param name="addedEntity">The added entity.</param> public async Task <AddedPermissableEntityResult> OnAddedAsync(IPermissable addedEntity) { Contract.Requires(addedEntity != null, "The added entity must not be null."); if (addedEntity.IsExempt()) { logger.Info("Permissable entity with id [{0]} and permissable type [{1}] was exempt from cam.", addedEntity.GetId(), addedEntity.GetPermissableType()); return(null); } else { var resourceTypeName = ResourceType.GetStaticLookup(addedEntity.GetPermissableType().GetResourceTypeId()); Contract.Assert(resourceTypeName != null, "The resource type should be recognized."); var resourceTypePermissions = await GetResourcePermissionsAsync(resourceTypeName.Value, null); var roles = await CreateGetAllRolesQuery().ToListAsync(); var existingResource = await ResourceQueries.CreateGetResourceByForeignResourceIdQuery(this.Context, addedEntity.GetId(), addedEntity.GetPermissableType().GetResourceTypeId()) .FirstOrDefaultAsync(); Resource parentResource = null; if (addedEntity.GetParentId().HasValue) { parentResource = await ResourceQueries.CreateGetResourceByForeignResourceIdQuery( this.Context, addedEntity.GetParentId().Value, addedEntity.GetParentPermissableType().GetResourceTypeId()) .FirstOrDefaultAsync(); } var addedPermissableEntityResult = DoOnAdded(addedEntity: addedEntity, existingResource: existingResource, parentResource: parentResource, resourceTypePermissions: resourceTypePermissions, roles: roles); logger.Info("Saving cam model context changes."); await this.Context.SaveChangesAsync(); return(addedPermissableEntityResult); } }
/// <summary> /// Updates the given permissable entity in CAM by updating it's parent resource if necessary. /// </summary> /// <param name="updatedEntity">The updated entity.</param> /// <returns>The task.</returns> public void OnUpdated(IPermissable updatedEntity) { if (updatedEntity.IsExempt()) { logger.Info("Permissable entity with id [{0]} and permissable type [{1}] was exempt from cam.", updatedEntity.GetId(), updatedEntity.GetPermissableType()); } else { var resource = ResourceQueries.CreateGetResourceByForeignResourceIdQuery(this.Context, updatedEntity.GetId(), updatedEntity.GetPermissableType().GetResourceTypeId()).FirstOrDefault(); Resource targetParentResource = null; Resource previousParentResource = null; if (updatedEntity.GetParentId().HasValue) { targetParentResource = ResourceQueries.CreateGetResourceByForeignResourceIdQuery(this.Context, updatedEntity.GetParentId().Value, updatedEntity.GetParentPermissableType().GetResourceTypeId()).FirstOrDefault(); } if (resource != null && resource.ParentResourceId.HasValue) { previousParentResource = Context.Resources.Find(resource.ParentResourceId.Value); } DoUpdate(updatedEntity, resource, targetParentResource, previousParentResource); this.Context.SaveChanges(); } }