/// <summary> /// Get filtered document Types /// </summary> private IEnumerable <DocumentTypeCache> GetFilteredDocumentTypes() { var contextEntity = this.ContextEntity(); if (contextEntity == null) { return(new List <DocumentTypeCache>()); } var entityTypeId = contextEntity.TypeId; List <DocumentTypeCache> documentypesForContextEntityType = DocumentTypeCache.GetByEntity(entityTypeId, true); // Get the document types allowed from the block settings and only have those in the list of document types for the entity if (GetAttributeValue(AttributeKeys.DocumentTypes).IsNotNullOrWhiteSpace()) { var blockAttributeFilteredDocumentTypes = GetAttributeValue(AttributeKeys.DocumentTypes).Split(',').Select(int.Parse).ToList(); documentypesForContextEntityType = documentypesForContextEntityType.Where(d => blockAttributeFilteredDocumentTypes.Contains(d.Id)).ToList(); } // Remove document types from the list that do not match the EntityTypeQualifiers var accessDeniedForDocumentTypeList = new List <int>(); foreach (var documentType in documentypesForContextEntityType) { // Check System Security on the type if (!documentType.IsAuthorized(Authorization.VIEW, this.CurrentPerson)) { accessDeniedForDocumentTypeList.Add(documentType.Id); continue; } // If the document does not have a qualifier column specified then allow it by default if (documentType.EntityTypeQualifierColumn.IsNotNullOrWhiteSpace()) { // Check that the EntityTypeQualifierColumn is a property for this entity, if not then remove it by default if (contextEntity.GetType().GetProperty(documentType.EntityTypeQualifierColumn) == null) { accessDeniedForDocumentTypeList.Add(documentType.Id); continue; } // Get the value of the property specified in DocumentType.EntityTypeQualifierColumn from the current ContextEntity string entityPropVal = this.ContextEntity().GetPropertyValue(documentType.EntityTypeQualifierColumn).ToString(); // If the entity property values does not match DocumentType.EntityTypeQualifierValue then it should be removed. if (entityPropVal != documentType.EntityTypeQualifierValue) { accessDeniedForDocumentTypeList.Add(documentType.Id); } } } // Create the list of document types that are valid for this entity, satisfy EntityTypeQualifiers, and that the current user has rights to view return(documentypesForContextEntityType.Where(d => !accessDeniedForDocumentTypeList.Contains(d.Id))); }
/// <summary> /// Executes the specified workflow. /// </summary> /// <param name="rockContext">The rock context.</param> /// <param name="action">The action.</param> /// <param name="entity">The entity.</param> /// <param name="errorMessages">The error messages.</param> /// <returns></returns> public override bool Execute(RockContext rockContext, WorkflowAction action, Object entity, out List <string> errorMessages) { errorMessages = new List <string>(); // Get the entity type EntityTypeCache entityType = null; var entityTypeGuid = GetAttributeValue(action, AttributeKey.EntityType).AsGuidOrNull(); if (entityTypeGuid.HasValue) { entityType = EntityTypeCache.Get(entityTypeGuid.Value); } if (entityType == null) { var message = $"Entity Type could not be found for selected value ('{entityTypeGuid.ToString()}')!"; errorMessages.Add(message); action.AddLogEntry(message, true); return(false); } var mergeFields = GetMergeFields(action); var _rockContext = new RockContext(); // Get the entity var entityTypeService = new EntityTypeService(_rockContext); IEntity entityObject = null; var entityIdGuidString = GetAttributeValue(action, AttributeKey.EntityIdOrGuid, true).ResolveMergeFields(mergeFields).Trim(); var entityId = entityIdGuidString.AsIntegerOrNull(); if (entityId.HasValue) { entityObject = entityTypeService.GetEntity(entityType.Id, entityId.Value); } else { var entityGuid = entityIdGuidString.AsGuidOrNull(); if (entityGuid.HasValue) { entityObject = entityTypeService.GetEntity(entityType.Id, entityGuid.Value); } } if (entityObject == null) { var value = GetActionAttributeValue(action, AttributeKey.EntityIdOrGuid); entityObject = action.GetEntityFromAttributeValue(value, rockContext); } if (entityObject == null) { var message = $"Entity could not be found for selected value ('{entityIdGuidString}')!"; errorMessages.Add(message); action.AddLogEntry(message, true); return(false); } var attributeFilteredDocumentType = GetAttributeValue(action, AttributeKey.DocumentType).Split(',').Select(int.Parse).FirstOrDefault(); var documentType = DocumentTypeCache.Get(attributeFilteredDocumentType); if (documentType == null) { var message = $"Document Type could not be found for selected value ('{attributeFilteredDocumentType}')!"; errorMessages.Add(message); action.AddLogEntry(message, true); return(false); } var documentypesForContextEntityType = DocumentTypeCache.GetByEntity(entityType.Id, true); if (!documentypesForContextEntityType.Any(d => attributeFilteredDocumentType == d.Id)) { var message = "The Document Type does not match the selected entity type."; errorMessages.Add(message); action.AddLogEntry(message, true); return(false); } var isDocumentTypeValid = IsDocumentTypeValidForEntity(entityObject, documentType); if (!isDocumentTypeValid) { var message = "The Document Type selected is not valid for the entity."; errorMessages.Add(message); action.AddLogEntry(message, true); return(false); } var binaryFile = new BinaryFileService(rockContext).Get(GetAttributeValue(action, AttributeKey.DocumentAttribute, true).AsGuid()); if (binaryFile == null) { action.AddLogEntry("The document to add to the entity was not found.", true); // returning true here to allow the action to run 'successfully' without a document. // This allows the action to be easily used when the document is optional without a bunch of action filter tests. return(true); } var documentName = GetAttributeValue(action, AttributeKey.DocumentName).ResolveMergeFields(mergeFields); if (documentName.IsNullOrWhiteSpace()) { documentName = Path.GetFileNameWithoutExtension(binaryFile.FileName); } var document = new Document(); document.Name = documentName; document.Description = GetAttributeValue(action, AttributeKey.DocumentDescription).ResolveMergeFields(mergeFields); document.EntityId = entityObject.Id; document.DocumentTypeId = documentType.Id; document.SetBinaryFile(binaryFile.Id, rockContext); if (!document.IsValidDocument(rockContext, out string errorMessage)) { errorMessages.Add(errorMessage); action.AddLogEntry(errorMessage, true); return(false); } var documentService = new DocumentService(rockContext); documentService.Add(document); rockContext.SaveChanges(); action.AddLogEntry("Added document to the Entity."); return(true); }