/// <summary> /// Returns a info object with basic authorization details for a resource by type and foreign resource id. /// </summary> /// <param name="resourceType">The resource type.</param> /// <param name="foreignResourceId">The resource by foreign resource id.</param> /// <returns>The resource authorization info dto.</returns> public async Task <ResourceAuthorizationInfoDTO> GetResourceAuthorizationInfoDTOAsync(string resourceType, int foreignResourceId) { var dto = await ResourceQueries.CreateGetResourceAuthorizationInfoDTOQuery(this.Context, resourceType, foreignResourceId).FirstOrDefaultAsync(); logger.Trace("Retrieved resource authorization info dto for resource type [{0}] and foreign resource id [{1}].", resourceType, foreignResourceId); return(dto); }
/// <summary> /// Get a ResourceId giving a foreignResourceId and a ResourceTypeId /// </summary> /// <param name="foreignResourceId">The foreign resource id.</param> /// <param name="resourceTypeId">The resource type.</param> /// <returns>The foreign resource cache.</returns> public ForeignResourceCache GetResourceByForeignResourceId(int foreignResourceId, int resourceTypeId) { var cache = GetCachedForeignResourceCache(foreignResourceId, resourceTypeId); if (cache != null) { return(cache); } else { var resource = ResourceQueries.CreateGetResourceByForeignResourceIdQuery(this.Context, foreignResourceId, resourceTypeId).FirstOrDefault(); int?parentResourceId = null; int?parentForeignResourceId = null; int?parentResourceTypeId = null; if (resource != null && resource.ParentResourceId.HasValue) { var parentResource = CreateGetResourceByResourceIdQuery(resource.ParentResourceId.Value).FirstOrDefault(); Contract.Assert(parentResource != null, "The parent resource should not be null."); parentResourceId = parentResource.ResourceId; parentForeignResourceId = parentResource.ForeignResourceId; parentResourceTypeId = parentResource.ResourceTypeId; } var resourceId = resource != null ? resource.ResourceId : default(int?); return(HandleNonCachedResource(foreignResourceId, resourceId, resourceTypeId, parentForeignResourceId, parentResourceId, parentResourceTypeId)); } }
/// <summary> /// Returns resource authorizations given the query operator. /// </summary> /// <param name="queryOperator">The query operator.</param> /// <returns>The paged filtered and sorted resource authorizations.</returns> public async Task <PagedQueryResults <ResourceAuthorization> > GetResourceAuthorizationsAsync(QueryableOperator <ResourceAuthorization> queryOperator) { var results = await ResourceQueries.CreateGetResourceAuthorizationsQuery(this.Context, queryOperator).ToPagedQueryResultsAsync(queryOperator.Start, queryOperator.Limit); logger.Trace("Retrieved resource authorizations using query operator [{0}].", queryOperator); return(results); }
public Payload<Resource> Delete(Resource obj) { // create payload var payload = new Payload<Resource>(); // todo: check security // validate var rules = new ValidationRules(); rules.ValidateHasId(obj); // assign errors from validation (if applicable) payload.AssignValidationErrors(rules.Errors); // check if valid if (rules.IsValid) { // db delete (inactive) using (var queries = new ResourceQueries()) { queries.Delete(ref obj); } payload.Data = obj; } // todo: next steps in workflow // return payload return payload; }
public override Dictionary <string, Sprite> GetImageOptions(int minTier) { Dictionary <string, Sprite> nameSpriteDict = new Dictionary <string, Sprite>(); // Get the resourceDefs with this type IEnumerable <ResourceDef> resourcesOfName = ResourceQueries.ByName(ManagerBase.resourceDefinitions, name); foreach (ResourceDef def in resourcesOfName) { nameSpriteDict.Add(def.name, def.image); } return(nameSpriteDict); }
/// <summary> /// Returns the permissions that can be set on a resource of the given type and resource id. If only the /// permissions for the resource type are needed, null can be passed for resource id. In this case, permission /// that have the same resource type but do have a resource id relationship will not be included. /// </summary> /// <param name="resourceType">The resource type.</param> /// <param name="foreignResourceId">The foreign resource id.</param> /// <returns>The available permissions for the given resource type and resource id.</returns> public async Task <List <ResourcePermissionDTO> > GetResourcePermissionsAsync(string resourceType, int?foreignResourceId) { throwIfResourceTypeIsNotKnown(resourceType); int?resourceId = null; if (foreignResourceId.HasValue) { resourceId = (await GetResourceByForeignResourceIdAsync(foreignResourceId.Value, ResourceType.GetStaticLookup(resourceType).Id)).ResourceId; logger.Trace("Retrieved resourceId [{0}] for foreign resource id [{1}].", resourceId, foreignResourceId); } var permissions = await ResourceQueries.CreateGetResourcePermissionsQuery(this.Context, resourceType, resourceId).ToListAsync(); logger.Trace("Retrieved resource permissions for resource type [{0}] and resource id [{1}].", resourceType, resourceId); return(permissions); }
/// <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(); } }
public Payload<List<Resource>> Get() { // create payload var payload = new Payload<List<Resource>>(); // todo: check security // get from db using (var queries = new ResourceQueries()) { payload.Data = queries.GetQuery().ToList(); } // return payload return payload; }
public Payload<List<Resource>> Search(string q) { // create payload var payload = new Payload<List<Resource>>(); // todo: check security // validate var rules = new ValidationRules(); rules.ValidateIsNotEmpty(q); // assign errors from validation (if applicable) payload.AssignValidationErrors(rules.Errors); // check if valid if (rules.IsValid) { // search db using (var queries = new ResourceQueries()) { payload.Data = queries.Search(q); } } // return payload return payload; }
public Payload<Resource> Save(Resource obj) { // create payload var payload = new Payload<Resource>(); // todo: check security // Prep obj bool isNewAsset = (obj.Id <= 0); business.SetDefaults(ref obj); // Check if we need to create new subject foreach (var association in obj.TopicAssociations) { // Get from db and see if it already exists var subjectLogic = new TopicAppLogic(); if (association.Topic == null) { // check if exists var subjectIsNew = (subjectLogic.Get(association.Tag).Data == null); if (subjectIsNew) { // create new subject association.Topic = new Topic() { Tag = association.Tag, ContributorGuid = association.ContributorGuid, Contributor = association.Contributor, Created = DateTime.Now }; } } } // validate var rules = new ValidationRules(); rules.Validate(obj); // assign errors from validation (if applicable) payload.AssignValidationErrors(rules.Errors); // check if valid if (rules.IsValid) { // if existing asset, check the properties that have changed prior to update var changedProperties = new StringBuilder(); if (!isNewAsset) { var originalAsset = Get(obj.Id).Data; CheckChangedProperties(originalAsset, obj, ref changedProperties); } // save to db using (var queries = new ResourceQueries()) { queries.Save(ref obj); } // assign primary data payload.Data = obj; // log activity if (isNewAsset) { // new asset AuditUtilities.Log(obj.Contributor, ActivityEventItem.ResourceCreated, string.Format(Resources.AuditEntries.ResourceCreated, obj.Contributor.Username)); } else { // updated asset AuditUtilities.Log(obj.Contributor, ActivityEventItem.ResourceModified, string.Format(Resources.AuditEntries.ResourceModified, obj.Contributor.Username, changedProperties)); } } // todo: next steps in workflow // return payload return payload; }
public Payload<List<Resource>> GetBySubject(string hashtag) { // create payload var payload = new Payload<List<Resource>>(); // todo: check security // get from db using (var queries = new ResourceQueries()) { payload.Data = queries.GetBySubject(hashtag); } // return payload return payload; }
public Payload<Resource> Get(int id) { // create payload var payload = new Payload<Resource>(); // todo: check security // get from db using (var queries = new ResourceQueries()) { payload.Data = queries.Get(id); } // Check if null to add error if (payload.Data == null) { payload.Errors.Add("00002", Resources.Errors.ERR00002); } // return payload return payload; }