private void AppyLogicAfter(EntityOperation operation, EntityOperationContext context, EntityOperationResult result) { foreach (var logic in _logics) { logic.After(operation, context, result); } }
public EntityOperationResult Delete(Services.tmp.EntityDelete delete) { using (var dbContext = _dbService.GetDatabaseContext(true)) { if (!RunInspection(delete)) { return EntityOperationResult.FailResult(new EntityOperationError("Insufficient rights to perform this operation.", EntityOperationError.ACCESS_VIOLATION)); } var ctx = new EntityOperationContext(); this.AppyLogicBefore(delete, ctx); EntityOperationResult result = null; try { _repository.Delete(new Entity(delete.Entity, delete.Id.Value), delete.Recursive); result = EntityOperationResult.SuccessResult(); } catch (RelationExistsException ree) { result = EntityOperationResult.FailResult(new EntityOperationError(ree.Message, EntityOperationError.RELATION_EXISTS)); } catch (Exception ex) { result = EntityOperationResult.FailResult(new EntityOperationError(ex.Message)); } this.AppyLogicAfter(delete, ctx, result); if (result.Success) dbContext.Complete(); return result; } }
private EntityOperationResult UpdateInternal(EntityUpdate update) { if (!RunInspection(update)) { return(EntityOperationResult.FailResult(new EntityOperationError("Insufficient rights to perform this operation.", EntityOperationError.ACCESS_VIOLATION))); } EntityOperationContext ctx = new EntityOperationContext(); this.AppyLogicBefore(update, ctx); var entity = update.ToEntity(); EntityOperationResult result = null; bool created = false; try { if (update.Id.HasValue) { if (update.PropertyUpdates.Count > 0) { _repository.Update(entity); } } else { update.Id = _repository.Create(entity); created = true; } //Order by operation - process detach first foreach (var relUpdate in update.RelationUpdates.OrderByDescending(ru => ru.Operation)) { if (relUpdate.Operation == RelationOperation.Attach) { _repository.Attach(entity, relUpdate.ToRelation()); } else if (relUpdate.Operation == RelationOperation.Detach) { _repository.Detach(entity, relUpdate.ToRelation()); } else if (relUpdate.PropertyUpdates.Count > 0) { _repository.UpdateRelation(entity, relUpdate.ToRelation()); } } result = EntityOperationResult.SuccessResult(); if (created) { result.Data.Add("Created", update.Id.Value); } } catch (UniqueRuleViolationException rex) { result = EntityOperationResult.FailResult(new EntityOperationError(rex.Message, EntityOperationError.UNIQUE_RULE_VIOLATION)); } this.AppyLogicAfter(update, ctx, result); return(result); }
public void After(EntityOperation operation, EntityOperationContext context, EntityOperationResult result) { if (operation.IsEntity(Notification.ENTITY) && operation is EntityUpdate && context.Get <bool>(CTXKEY_CREATENOTIFICATION) && result.Success) { var update = operation as EntityUpdate; var method = ReplyMethods.ByNotification;//default if (update.ContainsProperty("Method")) { method = update.Get <ReplyMethods>("Method"); } var recipientUpdate = update.GetRelationUpdate(User.ENTITY, Roles.Recipient); var attachments = update.GetMultipleRelationUpdates(NbuLibrary.Core.Domain.File.ENTITY, Roles.Attachment); if (attachments != null) { foreach (var att in attachments) { _fileService.GrantAccess(att.Id.Value, FileAccessType.Read, new User(recipientUpdate.Id.Value)); } } var recipientQuery = new EntityQuery2(User.ENTITY, recipientUpdate.Id.Value); recipientQuery.AddProperty("Email"); var recipient = _repository.Read(recipientQuery); var to = recipient.GetData <string>("Email"); var body = update.Get <string>("Body"); var subject = update.Get <string>("Subject"); } }
public void Before(EntityOperation operation, EntityOperationContext context) { if (operation.IsEntity(Notification.ENTITY) && operation is EntityUpdate) { var update = operation as EntityUpdate; if (update.IsCreate()) { update.Set("Date", DateTime.Now); var sender = update.GetRelationUpdate(User.ENTITY, Roles.Sender); if (sender == null) { update.Attach(User.ENTITY, Roles.Sender, _securityService.CurrentUser.Id); } else { sender.Id = _securityService.CurrentUser.Id; } context.Set <bool>(CTXKEY_CREATENOTIFICATION, true); } if (update.ContainsProperty("Body")) { var text = System.Web.HttpUtility.HtmlEncode(update.Get <string>("Body")); var newText = HtmlProcessor.ProcessEncodedHtml(text); update.Set("Body", newText); } } }
public void Before(EntityOperation operation, EntityOperationContext context) { var update = operation as EntityUpdate; if (update != null) { if (operation.IsEntity(EntityConsts.Issue)) { if (update.IsCreate() && !update.ContainsProperty("Year")) { update.Set("Year", DateTime.Now.Year); } if (update.ContainsProperty("Sent"))//TODO: mymagazines issue send (sent flag used) { context.Set <bool>(CTXKEY_SEND_ISSUE, true); } } else if (operation.IsEntity(EntityConsts.Magazine) && update.ContainsProperty("IsActive") && update.Id.HasValue) { if (update.Get <bool>("IsActive") == false) { var q = new EntityQuery2(EntityConsts.Magazine, update.Id.Value); q.AddProperty("IsActive"); var magazine = _repository.Read(q); context.Set <bool>(CTXKEY_ISACTIVEOLD, magazine.GetData <bool>("IsActive")); } } } }
public void Before(Services.tmp.EntityOperation operation, EntityOperationContext context) { if (operation.IsEntity(Payment.ENTITY) && operation is EntityUpdate) { var update = operation as EntityUpdate; if (update.IsCreate()) { List <int> usersToAttach = new List <int>(); foreach (var attach in update.RelationUpdates.Where(ru => ru.Operation == Services.tmp.RelationOperation.Attach)) { var em = _domainService.Domain.Entities[attach.Entity]; var customerRel = em.Relations[em.Name, User.ENTITY, Payment.ROLE_CUSTOMER]; if (customerRel != null && customerRel.TypeFor(User.ENTITY) == RelationType.OneToMany) { var q = new EntityQuery2(User.ENTITY); q.WhereRelated(new RelationQuery(em.Name, customerRel.Role, attach.Id.Value)); var cust = _repository.Read(q); if (cust != null) { usersToAttach.Add(cust.Id); } } } foreach (var id in usersToAttach) { update.Attach(User.ENTITY, Payment.ROLE_CUSTOMER, id); } } } }
public void After(EntityOperation operation, EntityOperationContext context, EntityOperationResult result) { if (!result.Success) { return; } }
private void AppyLogicBefore(EntityOperation operation, EntityOperationContext context) { foreach (var logic in _logics) { logic.Before(operation, context); } }
public void Before(Core.Services.tmp.EntityOperation operation, EntityOperationContext context) { if (operation.IsEntity(EntityConsts.BibliographicListQuery)) { if (operation is EntityUpdate) { var update = operation as EntityUpdate; if (update.IsCreate() && _securityService.CurrentUser.UserType == UserTypes.Customer) { update.Attach(User.ENTITY, Roles.Customer, _securityService.CurrentUser.Id); } else if (_securityService.CurrentUser.UserType == UserTypes.Librarian) { bool attach = false; int? fileId = null; if (update.IsCreate()) { attach = true; } else { var q = new EntityQuery2(EntityConsts.BibliographicListQuery, update.Id.Value); q.Include(User.ENTITY, Roles.ProcessedBy); q.Include(File.ENTITY, Roles.File); var e = _repository.Read(q); var user = e.GetSingleRelation(User.ENTITY, Roles.ProcessedBy); if (user == null) { attach = true; } else if (user.Entity.Id != _securityService.CurrentUser.Id) { update.Detach(User.ENTITY, Roles.ProcessedBy, user.Id); attach = true; } var file = e.GetSingleRelation(File.ENTITY, Roles.File); if (file != null) { fileId = file.Entity.Id; } } if (attach) { update.Attach(User.ENTITY, Roles.ProcessedBy, _securityService.CurrentUser.Id); if (fileId.HasValue) { var librarian = _securityService.CurrentUser; using (_securityService.BeginSystemContext()) { _fileService.GrantAccess(fileId.Value, FileAccessType.Full, librarian); } } } } } } }
public void After(Core.Services.tmp.EntityOperation operation, EntityOperationContext context, EntityOperationResult result) { if (!result.Success) { return; } var update = operation as EntityUpdate; if (operation.IsEntity(EntityConsts.BibliographicListQuery) && update != null && update.ContainsProperty("Status") && update.Get <QueryStatus>("Status") == QueryStatus.Completed) { var q = new EntityQuery2(EntityConsts.BibliographicListQuery, update.Id.Value) { AllProperties = true }; q.Include(User.ENTITY, Roles.Customer); var biblListQuery = _repository.Read(q); var user = new User(biblListQuery.GetSingleRelation(User.ENTITY, Roles.Customer).Entity); var template = _templateService.Get(new Guid(NotificationTemplates.QUERY_COMPLETED)); string subject = null, body = null; Dictionary <string, Entity> templateContext = new Dictionary <string, Entity>(StringComparer.InvariantCultureIgnoreCase); templateContext.Add("Customer", user); templateContext.Add("Query", biblListQuery); _templateService.Render(template, templateContext, out subject, out body); var withEmail = biblListQuery.GetData <ReplyMethods>("ReplyMethod") == ReplyMethods.ByEmail; _notificationService.SendNotification(withEmail, new User[] { user }, subject, body, null, new Relation[] { new Relation(Notification.ROLE, biblListQuery) }); } else if (operation.IsEntity(Payment.ENTITY) && update != null && update.ContainsProperty("Status") && update.Get <PaymentStatus>("Status") == PaymentStatus.Paid) { var q = new EntityQuery2(EntityConsts.BibliographicListQuery); q.AddProperties("Number"); q.WhereRelated(new RelationQuery(Payment.ENTITY, Roles.Payment, update.Id.Value)); q.Include(User.ENTITY, Roles.Customer); q.Include(File.ENTITY, Roles.File); var biblListQuery = _repository.Read(q); if (biblListQuery != null) { var file = new File(biblListQuery.GetSingleRelation(File.ENTITY, Roles.File).Entity); var user = new User(biblListQuery.GetSingleRelation(User.ENTITY, Roles.Customer).Entity); var template = _templateService.Get(new Guid(NotificationTemplates.PAYMENT_COMPLETED)); string subject = null, body = null; Dictionary <string, Entity> templateContext = new Dictionary <string, Entity>(StringComparer.InvariantCultureIgnoreCase); templateContext.Add("Customer", user); templateContext.Add("Query", biblListQuery); _templateService.Render(template, templateContext, out subject, out body); var withEmail = biblListQuery.GetData <ReplyMethods>("ReplyMethod") == ReplyMethods.ByEmail; _notificationService.SendNotification(withEmail, new User[] { user }, subject, body, new File[] { file }, new Relation[] { new Relation(Notification.ROLE, biblListQuery) }); //_fileService.GrantAccess(file.Id, FileAccessType.Read, new User(biblQuery.GetSingleRelation(User.ENTITY, Roles.Customer).Entity)); } } }
static ProcessEntity ProcessOperation_Plan(EntityOperationContext <ProcessEntity> args) { DateTime plan = TimeZoneManager.Now; if (ValueLineBox.Show(ref plan, "Choose planned date", "Please, choose the date you want the process to start", "Planned date", null, null, Window.GetWindow(args.SenderButton))) { return(((ProcessEntity)args.Entity).ToLite().ExecuteLite(ProcessOperation.Plan, plan)); } return(null); }
public void Before(Services.tmp.EntityOperation operation, EntityOperationContext context) { if (operation is EntityUpdate) { var update = operation as EntityUpdate; if (update.IsCreate()) { update.Set(Properties.CreatedBy, _securityService.CurrentUser.Id); update.Set(Properties.CreatedOn, DateTime.Now); } } }
public void After(Services.tmp.EntityOperation operation, EntityOperationContext context, EntityOperationResult result) { if (operation.IsEntity(NbuLibrary.Core.Domain.File.ENTITY) && operation is EntityDelete && result.Success) { var file = context.Get <NbuLibrary.Core.Domain.File>(CTXKEY_FILEDELETION); if (file != null) { _fileService.DeleteFileContent(Guid.Parse(file.ContentPath)); } } }
public void Before(EntityOperation operation, EntityOperationContext context) { if (!operation.IsEntity(Inquery.EntityType)) { return; } if (operation is EntityUpdate) { var update = operation as EntityUpdate; if (_securityService.CurrentUser.UserType == UserTypes.Customer && update.IsCreate()) { update.Attach(User.ENTITY, RelationConsts.Customer, _securityService.CurrentUser.Id); } else if (_securityService.CurrentUser.UserType == UserTypes.Librarian) { bool attach = false; if (update.IsCreate()) { attach = true; } else { var q = new EntityQuery2(User.ENTITY); q.WhereRelated(new RelationQuery(Inquery.EntityType, RelationConsts.ProcessedBy, update.Id.Value)); var user = _repository.Read(q); if (user == null) { attach = true; } else if (user.Id != _securityService.CurrentUser.Id) { update.Detach(User.ENTITY, RelationConsts.ProcessedBy, user.Id); attach = true; } } if (attach) { update.Attach(User.ENTITY, RelationConsts.ProcessedBy, _securityService.CurrentUser.Id); } } } }
public void Before(EntityOperation operation, EntityOperationContext context) { if (operation is EntityUpdate && operation.IsEntity(User.ENTITY)) { var update = operation as EntityUpdate; if (update.IsCreate() && !update.ContainsProperty("Password")) //generate random password { update.Set("Password", GenerateRandomPassword()); } if (update.ContainsProperty("password")) { var newPassword = update.PropertyUpdates["password"] as string; context.Set <string>(CTXKEY_PASSWORD_UPDATE, newPassword); string hash = null; using (SHA1 sha1 = SHA1.Create()) { hash = Convert.ToBase64String(sha1.ComputeHash(Encoding.UTF8.GetBytes(newPassword))); } update.Set("password", hash); update.Set("FailedLoginsCount", 0); } if (update.ContainsProperty("RecoveryCode")) { context.Set <bool>(CTXKEY_USER_PASSWORDRECOVERY, true); } if (update.IsCreate()) { context.Set <bool>(CTXKEY_USER_CREATION, true); } else if (update.Id.Value == _securityService.CurrentUser.Id) { context.Set <int>(CTXKEY_UPDATE_PROFILE, _securityService.CurrentUser.Id); } } }
public static Entity FindAssociatedTemplates(EntityOperationContext <Entity> e) { var template = Finder.Find(new FindOptions(typeof(SMSTemplateEntity)) { FilterOptions = new List <FilterOption> { { new FilterOption("IsActive", true) { Frozen = true } }, { new FilterOption("AssociatedType", Server.ServerTypes[e.Entity.GetType()]) } }, SearchOnLoad = true, }); if (template != null) { Navigator.Navigate(e.Entity.ToLite().ConstructFromLite(SMSMessageOperation.CreateSMSWithTemplateFromEntity, ((Lite <SMSTemplateEntity>)template).Retrieve())); } return(null); }
public EntityOperationResult Delete(Services.tmp.EntityDelete delete) { using (var dbContext = _dbService.GetDatabaseContext(true)) { if (!RunInspection(delete)) { return(EntityOperationResult.FailResult(new EntityOperationError("Insufficient rights to perform this operation.", EntityOperationError.ACCESS_VIOLATION))); } var ctx = new EntityOperationContext(); this.AppyLogicBefore(delete, ctx); EntityOperationResult result = null; try { _repository.Delete(new Entity(delete.Entity, delete.Id.Value), delete.Recursive); result = EntityOperationResult.SuccessResult(); } catch (RelationExistsException ree) { result = EntityOperationResult.FailResult(new EntityOperationError(ree.Message, EntityOperationError.RELATION_EXISTS)); } catch (Exception ex) { result = EntityOperationResult.FailResult(new EntityOperationError(ex.Message)); } this.AppyLogicAfter(delete, ctx, result); if (result.Success) { dbContext.Complete(); } return(result); } }
public void After(Services.tmp.EntityOperation operation, EntityOperationContext context, EntityOperationResult result) { if (!result.Success) { return; } using (var dbContext = _dbService.GetDatabaseContext(true)) { if (operation is EntityDelete) { LogOperation(operation.Entity, operation.Id.Value, HistoryOperation.Deleted, DateTime.Now, dbContext.Connection); } else if (operation is EntityUpdate) { var update = operation as EntityUpdate; bool created = result.Data.ContainsKey("Created"); var now = DateTime.Now; int opId = LogOperation(update.Entity, update.Id.Value, created ? HistoryOperation.Created : HistoryOperation.Modified, now, dbContext.Connection); foreach (var propUpdate in update.PropertyUpdates) { LogPropertyChange(opId, propUpdate.Key, propUpdate.Value, dbContext.Connection); } foreach (var relUpdate in update.RelationUpdates) { int relChangeId = LogRelationChange(opId, relUpdate.Entity, relUpdate.Role, relUpdate.Id.Value, relUpdate.Operation, dbContext.Connection); foreach (var propUpdate in relUpdate.PropertyUpdates) { LogRelationPropertyChange(relChangeId, propUpdate.Key, propUpdate.Value, dbContext.Connection); } } } dbContext.Complete(); } }
public void Before(Services.tmp.EntityOperation operation, EntityOperationContext context) { if (operation.IsEntity(NbuLibrary.Core.Domain.File.ENTITY) && operation is EntityUpdate) { var update = operation as EntityUpdate; if (update.IsCreate()) { var access = update.Attach(NbuLibrary.Core.Domain.User.ENTITY, Roles.Access, _securityService.CurrentUser.Id); access.Set("Type", FileAccessType.Owner); } else { var accessUpdates = update.GetMultipleRelationUpdates(User.ENTITY, Roles.Access); if (accessUpdates != null) { foreach (var ac in accessUpdates) { if (ac.Operation == RelationOperation.Detach) { continue; } if (ac.ContainsProperty("Type") && ac.Get <FileAccessType>("Type") == FileAccessType.Token) { ac.Set("Token", Guid.NewGuid());//TODO: token based file access } } } } } else if (operation.IsEntity(NbuLibrary.Core.Domain.File.ENTITY) && operation is EntityDelete) { var file = _fileService.GetFile(operation.Id.Value); context.Set <NbuLibrary.Core.Domain.File>(CTXKEY_FILEDELETION, file); } }
public void After(EntityOperation operation, EntityOperationContext context, EntityOperationResult result) { if (!result.Success) { return; } if (operation.IsEntity(EntityConsts.Issue) && operation is EntityUpdate) { var update = operation as EntityUpdate; if (context.Get <bool>(CTXKEY_SEND_ISSUE)) { SendIssueToSubscribers(operation as EntityUpdate); } if (update.ContainsRelation(File.ENTITY, Roles.Content)) { var filesAttached = update.GetMultipleRelationUpdates(File.ENTITY, Roles.Content).Where(fu => fu.Operation == RelationOperation.Attach); if (filesAttached.Count() > 0) { var issue = update.ToEntity(); var q = new EntityQuery2(EntityConsts.Magazine); q.WhereRelated(new RelationQuery(EntityConsts.Issue, Roles.Issue, issue.Id)); q.Include(User.ENTITY, Roles.Subscriber); var mag = _repository.Read(q); var subscribers = mag.GetManyRelations(User.ENTITY, Roles.Subscriber).Select(r => new User(r.Entity)); foreach (var subscriber in subscribers) { foreach (var fileUpdate in filesAttached) { if (!_fileService.HasAccess(subscriber, fileUpdate.Id.Value)) { _fileService.GrantAccess(fileUpdate.Id.Value, FileAccessType.Read, subscriber); } } } } } } else if (operation is EntityUpdate) { var update = operation as EntityUpdate; if (update.IsEntity(User.ENTITY) && update.ContainsRelation(EntityConsts.Magazine, Roles.Subscriber)) { var rus = update.GetMultipleRelationUpdates(EntityConsts.Magazine, Roles.Subscriber).Where(ru => ru.Operation == RelationOperation.Attach); foreach (var ru in rus) { var q = new EntityQuery2(EntityConsts.Issue); q.WhereRelated(new RelationQuery(EntityConsts.Magazine, Roles.Issue, ru.Id.Value)); q.Include(File.ENTITY, Roles.Content); var issues = _repository.Search(q); foreach (var issue in issues) { //The user cannot give himself an access to file - only owner or administrator can. using (_securityService.BeginSystemContext()) { GiveFileAccessForIssue(issue, new User(update.ToEntity())); } } } } else if (update.IsEntity(EntityConsts.Magazine) && update.ContainsRelation(User.ENTITY, Roles.Subscriber)) { var rus = update.GetMultipleRelationUpdates(User.ENTITY, Roles.Subscriber).Where(ru => ru.Operation == RelationOperation.Attach); if (rus.Count() > 0) { var q = new EntityQuery2(EntityConsts.Issue); q.WhereRelated(new RelationQuery(EntityConsts.Magazine, Roles.Issue, update.Id.Value)); q.Include(File.ENTITY, Roles.Content); var issues = _repository.Search(q); foreach (var ru in rus) { foreach (var issue in issues) { GiveFileAccessForIssue(issue, new User(ru.Id.Value)); } } } } else if (update.IsEntity(EntityConsts.Magazine) && update.ContainsProperty("IsActive")) { var isActiveNew = update.Get <bool>("IsActive"); if (isActiveNew == false && context.Get <bool>(CTXKEY_ISACTIVEOLD)) { SendMagazineNotActiveToSubscribers(update); } } } }
public void Before(Core.Services.tmp.EntityOperation operation, EntityOperationContext context) { if (operation.IsEntity(EntityConsts.BibliographicListQuery)) { if (operation is EntityUpdate) { var update = operation as EntityUpdate; if (update.IsCreate() && _securityService.CurrentUser.UserType == UserTypes.Customer) update.Attach(User.ENTITY, Roles.Customer, _securityService.CurrentUser.Id); else if (_securityService.CurrentUser.UserType == UserTypes.Librarian) { bool attach = false; int? fileId = null; if (update.IsCreate()) attach = true; else { var q = new EntityQuery2(EntityConsts.BibliographicListQuery, update.Id.Value); q.Include(User.ENTITY, Roles.ProcessedBy); q.Include(File.ENTITY, Roles.File); var e = _repository.Read(q); var user = e.GetSingleRelation(User.ENTITY, Roles.ProcessedBy); if (user == null) attach = true; else if (user.Entity.Id != _securityService.CurrentUser.Id) { update.Detach(User.ENTITY, Roles.ProcessedBy, user.Id); attach = true; } var file = e.GetSingleRelation(File.ENTITY, Roles.File); if (file != null) fileId = file.Entity.Id; } if (attach) { update.Attach(User.ENTITY, Roles.ProcessedBy, _securityService.CurrentUser.Id); if (fileId.HasValue) { var librarian = _securityService.CurrentUser; using (_securityService.BeginSystemContext()) { _fileService.GrantAccess(fileId.Value, FileAccessType.Full, librarian); } } } } } } }
public void After(Core.Services.tmp.EntityOperation operation, EntityOperationContext context, EntityOperationResult result) { if (!result.Success) return; var update = operation as EntityUpdate; if (operation.IsEntity(EntityConsts.BibliographicListQuery) && update != null && update.ContainsProperty("Status") && update.Get<QueryStatus>("Status") == QueryStatus.Completed) { var q = new EntityQuery2(EntityConsts.BibliographicListQuery, update.Id.Value) { AllProperties = true }; q.Include(User.ENTITY, Roles.Customer); var biblListQuery = _repository.Read(q); var user = new User(biblListQuery.GetSingleRelation(User.ENTITY, Roles.Customer).Entity); var template = _templateService.Get(new Guid(NotificationTemplates.QUERY_COMPLETED)); string subject = null, body = null; Dictionary<string, Entity> templateContext = new Dictionary<string, Entity>(StringComparer.InvariantCultureIgnoreCase); templateContext.Add("Customer", user); templateContext.Add("Query", biblListQuery); _templateService.Render(template, templateContext, out subject, out body); var withEmail = biblListQuery.GetData<ReplyMethods>("ReplyMethod") == ReplyMethods.ByEmail; _notificationService.SendNotification(withEmail, new User[] { user }, subject, body, null, new Relation[] { new Relation(Notification.ROLE, biblListQuery) }); } else if (operation.IsEntity(Payment.ENTITY) && update != null && update.ContainsProperty("Status") && update.Get<PaymentStatus>("Status") == PaymentStatus.Paid) { var q = new EntityQuery2(EntityConsts.BibliographicListQuery); q.AddProperties("Number"); q.WhereRelated(new RelationQuery(Payment.ENTITY, Roles.Payment, update.Id.Value)); q.Include(User.ENTITY, Roles.Customer); q.Include(File.ENTITY, Roles.File); var biblListQuery = _repository.Read(q); if (biblListQuery != null) { var file = new File(biblListQuery.GetSingleRelation(File.ENTITY, Roles.File).Entity); var user = new User(biblListQuery.GetSingleRelation(User.ENTITY, Roles.Customer).Entity); var template = _templateService.Get(new Guid(NotificationTemplates.PAYMENT_COMPLETED)); string subject = null, body = null; Dictionary<string, Entity> templateContext = new Dictionary<string, Entity>(StringComparer.InvariantCultureIgnoreCase); templateContext.Add("Customer", user); templateContext.Add("Query", biblListQuery); _templateService.Render(template, templateContext, out subject, out body); var withEmail = biblListQuery.GetData<ReplyMethods>("ReplyMethod") == ReplyMethods.ByEmail; _notificationService.SendNotification(withEmail, new User[] { user }, subject, body, new File[] { file }, new Relation[] { new Relation(Notification.ROLE, biblListQuery) }); //_fileService.GrantAccess(file.Id, FileAccessType.Read, new User(biblQuery.GetSingleRelation(User.ENTITY, Roles.Customer).Entity)); } } }
public void Before(EntityOperation operation, EntityOperationContext context) { if (!operation.IsEntity(Inquery.EntityType)) return; if (operation is EntityUpdate) { var update = operation as EntityUpdate; if (_securityService.CurrentUser.UserType == UserTypes.Customer && update.IsCreate()) { update.Attach(User.ENTITY, RelationConsts.Customer, _securityService.CurrentUser.Id); } else if (_securityService.CurrentUser.UserType == UserTypes.Librarian) { bool attach = false; if (update.IsCreate()) attach = true; else { var q = new EntityQuery2(User.ENTITY); q.WhereRelated(new RelationQuery(Inquery.EntityType, RelationConsts.ProcessedBy, update.Id.Value)); var user = _repository.Read(q); if (user == null) attach = true; else if (user.Id != _securityService.CurrentUser.Id) { update.Detach(User.ENTITY, RelationConsts.ProcessedBy, user.Id); attach = true; } } if (attach) update.Attach(User.ENTITY, RelationConsts.ProcessedBy, _securityService.CurrentUser.Id); } } }
private void AppyLogicAfter(EntityOperation operation, EntityOperationContext context, EntityOperationResult result) { foreach (var logic in _logics) logic.After(operation, context, result); }
public void After(EntityOperation operation, EntityOperationContext context, EntityOperationResult result) { if (operation.IsEntity(Notification.ENTITY) && operation is EntityUpdate && context.Get<bool>(CTXKEY_CREATENOTIFICATION) && result.Success) { var update = operation as EntityUpdate; var method = ReplyMethods.ByNotification;//default if (update.ContainsProperty("Method")) method = update.Get<ReplyMethods>("Method"); var recipientUpdate = update.GetRelationUpdate(User.ENTITY, Roles.Recipient); var attachments = update.GetMultipleRelationUpdates(NbuLibrary.Core.Domain.File.ENTITY, Roles.Attachment); if (attachments != null) foreach (var att in attachments) { _fileService.GrantAccess(att.Id.Value, FileAccessType.Read, new User(recipientUpdate.Id.Value)); } var recipientQuery = new EntityQuery2(User.ENTITY, recipientUpdate.Id.Value); recipientQuery.AddProperty("Email"); var recipient = _repository.Read(recipientQuery); var to = recipient.GetData<string>("Email"); var body = update.Get<string>("Body"); var subject = update.Get<string>("Subject"); } }
public void Before(EntityOperation operation, EntityOperationContext context) { var update = operation as EntityUpdate; if (update != null) { if (operation.IsEntity(EntityConsts.Issue)) { if (update.IsCreate() && !update.ContainsProperty("Year")) update.Set("Year", DateTime.Now.Year); if (update.ContainsProperty("Sent"))//TODO: mymagazines issue send (sent flag used) { context.Set<bool>(CTXKEY_SEND_ISSUE, true); } } else if (operation.IsEntity(EntityConsts.Magazine) && update.ContainsProperty("IsActive") && update.Id.HasValue) { if (update.Get<bool>("IsActive") == false) { var q = new EntityQuery2(EntityConsts.Magazine, update.Id.Value); q.AddProperty("IsActive"); var magazine = _repository.Read(q); context.Set<bool>(CTXKEY_ISACTIVEOLD, magazine.GetData<bool>("IsActive")); } } } }
public void After(Services.tmp.EntityOperation operation, EntityOperationContext context, EntityOperationResult result) { if (!result.Success) return; using (var dbContext = _dbService.GetDatabaseContext(true)) { if (operation is EntityDelete) { LogOperation(operation.Entity, operation.Id.Value, HistoryOperation.Deleted, DateTime.Now, dbContext.Connection); } else if (operation is EntityUpdate) { var update = operation as EntityUpdate; bool created = result.Data.ContainsKey("Created"); var now = DateTime.Now; int opId = LogOperation(update.Entity, update.Id.Value, created ? HistoryOperation.Created : HistoryOperation.Modified, now, dbContext.Connection); foreach (var propUpdate in update.PropertyUpdates) { LogPropertyChange(opId, propUpdate.Key, propUpdate.Value, dbContext.Connection); } foreach (var relUpdate in update.RelationUpdates) { int relChangeId = LogRelationChange(opId, relUpdate.Entity, relUpdate.Role, relUpdate.Id.Value, relUpdate.Operation, dbContext.Connection); foreach (var propUpdate in relUpdate.PropertyUpdates) { LogRelationPropertyChange(relChangeId, propUpdate.Key, propUpdate.Value, dbContext.Connection); } } } dbContext.Complete(); } }
private EntityOperationResult UpdateInternal(EntityUpdate update) { if (!RunInspection(update)) return EntityOperationResult.FailResult(new EntityOperationError("Insufficient rights to perform this operation.", EntityOperationError.ACCESS_VIOLATION)); EntityOperationContext ctx = new EntityOperationContext(); this.AppyLogicBefore(update, ctx); var entity = update.ToEntity(); EntityOperationResult result = null; bool created = false; try { if (update.Id.HasValue) { if (update.PropertyUpdates.Count > 0) _repository.Update(entity); } else { update.Id = _repository.Create(entity); created = true; } //Order by operation - process detach first foreach (var relUpdate in update.RelationUpdates.OrderByDescending(ru => ru.Operation)) { if (relUpdate.Operation == RelationOperation.Attach) _repository.Attach(entity, relUpdate.ToRelation()); else if (relUpdate.Operation == RelationOperation.Detach) _repository.Detach(entity, relUpdate.ToRelation()); else if (relUpdate.PropertyUpdates.Count > 0) { _repository.UpdateRelation(entity, relUpdate.ToRelation()); } } result = EntityOperationResult.SuccessResult(); if (created) result.Data.Add("Created", update.Id.Value); } catch (UniqueRuleViolationException rex) { result = EntityOperationResult.FailResult(new EntityOperationError(rex.Message, EntityOperationError.UNIQUE_RULE_VIOLATION)); } this.AppyLogicAfter(update, ctx, result); return result; }
public void Before(EntityOperation operation, EntityOperationContext context) { if (operation is EntityUpdate && operation.IsEntity(User.ENTITY)) { var update = operation as EntityUpdate; if (update.IsCreate() && !update.ContainsProperty("Password")) //generate random password { update.Set("Password", GenerateRandomPassword()); } if (update.ContainsProperty("password")) { var newPassword = update.PropertyUpdates["password"] as string; context.Set<string>(CTXKEY_PASSWORD_UPDATE, newPassword); string hash = null; using (SHA1 sha1 = SHA1.Create()) { hash = Convert.ToBase64String(sha1.ComputeHash(Encoding.UTF8.GetBytes(newPassword))); } update.Set("password", hash); update.Set("FailedLoginsCount", 0); } if (update.ContainsProperty("RecoveryCode")) { context.Set<bool>(CTXKEY_USER_PASSWORDRECOVERY, true); } if (update.IsCreate()) context.Set<bool>(CTXKEY_USER_CREATION, true); else if (update.Id.Value == _securityService.CurrentUser.Id) context.Set<int>(CTXKEY_UPDATE_PROFILE, _securityService.CurrentUser.Id); } }
public void Before(EntityOperation operation, EntityOperationContext context) { if (operation.IsEntity(Notification.ENTITY) && operation is EntityUpdate) { var update = operation as EntityUpdate; if (update.IsCreate()) { update.Set("Date", DateTime.Now); var sender = update.GetRelationUpdate(User.ENTITY, Roles.Sender); if (sender == null) update.Attach(User.ENTITY, Roles.Sender, _securityService.CurrentUser.Id); else sender.Id = _securityService.CurrentUser.Id; context.Set<bool>(CTXKEY_CREATENOTIFICATION, true); } if (update.ContainsProperty("Body")) { var text = System.Web.HttpUtility.HtmlEncode(update.Get<string>("Body")); var newText = HtmlProcessor.ProcessEncodedHtml(text); update.Set("Body", newText); } } }
public void After(EntityOperation operation, EntityOperationContext context, EntityOperationResult result) { if (!result.Success) return; if (operation.IsEntity(EntityConsts.Issue) && operation is EntityUpdate) { var update = operation as EntityUpdate; if (context.Get<bool>(CTXKEY_SEND_ISSUE)) { SendIssueToSubscribers(operation as EntityUpdate); } if (update.ContainsRelation(File.ENTITY, Roles.Content)) { var filesAttached = update.GetMultipleRelationUpdates(File.ENTITY, Roles.Content).Where(fu => fu.Operation == RelationOperation.Attach); if (filesAttached.Count() > 0) { var issue = update.ToEntity(); var q = new EntityQuery2(EntityConsts.Magazine); q.WhereRelated(new RelationQuery(EntityConsts.Issue, Roles.Issue, issue.Id)); q.Include(User.ENTITY, Roles.Subscriber); var mag = _repository.Read(q); var subscribers = mag.GetManyRelations(User.ENTITY, Roles.Subscriber).Select(r => new User(r.Entity)); foreach (var subscriber in subscribers) { foreach (var fileUpdate in filesAttached) { if (!_fileService.HasAccess(subscriber, fileUpdate.Id.Value)) _fileService.GrantAccess(fileUpdate.Id.Value, FileAccessType.Read, subscriber); } } } } } else if (operation is EntityUpdate) { var update = operation as EntityUpdate; if (update.IsEntity(User.ENTITY) && update.ContainsRelation(EntityConsts.Magazine, Roles.Subscriber)) { var rus = update.GetMultipleRelationUpdates(EntityConsts.Magazine, Roles.Subscriber).Where(ru => ru.Operation == RelationOperation.Attach); foreach (var ru in rus) { var q = new EntityQuery2(EntityConsts.Issue); q.WhereRelated(new RelationQuery(EntityConsts.Magazine, Roles.Issue, ru.Id.Value)); q.Include(File.ENTITY, Roles.Content); var issues = _repository.Search(q); foreach (var issue in issues) { //The user cannot give himself an access to file - only owner or administrator can. using (_securityService.BeginSystemContext()) { GiveFileAccessForIssue(issue, new User(update.ToEntity())); } } } } else if (update.IsEntity(EntityConsts.Magazine) && update.ContainsRelation(User.ENTITY, Roles.Subscriber)) { var rus = update.GetMultipleRelationUpdates(User.ENTITY, Roles.Subscriber).Where(ru => ru.Operation == RelationOperation.Attach); if (rus.Count() > 0) { var q = new EntityQuery2(EntityConsts.Issue); q.WhereRelated(new RelationQuery(EntityConsts.Magazine, Roles.Issue, update.Id.Value)); q.Include(File.ENTITY, Roles.Content); var issues = _repository.Search(q); foreach (var ru in rus) { foreach (var issue in issues) GiveFileAccessForIssue(issue, new User(ru.Id.Value)); } } } else if (update.IsEntity(EntityConsts.Magazine) && update.ContainsProperty("IsActive")) { var isActiveNew = update.Get<bool>("IsActive"); if (isActiveNew == false && context.Get<bool>(CTXKEY_ISACTIVEOLD)) { SendMagazineNotActiveToSubscribers(update); } } } }
public void After(EntityOperation operation, EntityOperationContext context, EntityOperationResult result) { if (!result.Success) { return; } if (!operation.IsEntity(User.ENTITY)) { return; } var update = operation as EntityUpdate; if (update == null) { return; } if (context.Get <bool>(CTXKEY_USER_PASSWORDRECOVERY)) { var user = new User(_repository.Read(new EntityQuery2(update.Entity, update.Id.Value) { AllProperties = true })); var template = _templateService.Get(new Guid(NotificationTemplates.USER_PASSWORDRECOVERY)); string subject = null, body = null; Dictionary <string, Entity> templateContext = new Dictionary <string, Entity>(StringComparer.InvariantCultureIgnoreCase); templateContext.Add("User", user); _templateService.Render(template, templateContext, out subject, out body); //TODO: async execution _notificationService.SendEmail(user.Email, subject, body, null); } if (context.Get <bool>(CTXKEY_USER_CREATION)) { var user = new User(_repository.Read(new EntityQuery2(update.Entity, update.Id.Value) { AllProperties = true })); var pwd = context.Get <string>(CTXKEY_PASSWORD_UPDATE); user.SetData <String>("Password", pwd); var template = _templateService.Get(new Guid(NotificationTemplates.USER_CREATED)); string subject = null, body = null; Dictionary <string, Entity> templateContext = new Dictionary <string, Entity>(StringComparer.InvariantCultureIgnoreCase); templateContext.Add("User", user); _templateService.Render(template, templateContext, out subject, out body); //TODO: async execution _notificationService.SendEmail(user.Email, subject, body, null); if (update.ContainsProperty("IsActive") && update.Get <bool>("IsActive")) { SendUserActivationEmail(user); } } else if (update.ContainsProperty("IsActive") && update.Get <bool>("IsActive")) { var user = new User(_repository.Read(new EntityQuery2(update.Entity, update.Id.Value) { AllProperties = true })); SendUserActivationEmail(user); } else if (context.Get <int>(CTXKEY_UPDATE_PROFILE) > 0) { if (update.ContainsRelation("UserGroup", "UserGroup") || update.ContainsProperty("FacultyNumber") || update.ContainsProperty("CardNumber")) { var user = new User(context.Get <int>(CTXKEY_UPDATE_PROFILE)); user.IsActive = false; _repository.Update(user); _securityService.Logout(); result.Data.Add("account_warning_logged_out", true); } if (update.ContainsProperty("Email")) { _securityService.UpdateCurrentUserEmail(update.Get <string>("Email")); result.Data.Add("account_event_email_changed", update.Get <string>("Email")); } } }
public void After(Services.tmp.EntityOperation operation, EntityOperationContext context, EntityOperationResult result) { }
public void Before(Services.tmp.EntityOperation operation, EntityOperationContext context) { if (operation.IsEntity(Payment.ENTITY) && operation is EntityUpdate) { var update = operation as EntityUpdate; if (update.IsCreate()) { List<int> usersToAttach = new List<int>(); foreach (var attach in update.RelationUpdates.Where(ru => ru.Operation == Services.tmp.RelationOperation.Attach)) { var em = _domainService.Domain.Entities[attach.Entity]; var customerRel = em.Relations[em.Name, User.ENTITY, Payment.ROLE_CUSTOMER]; if (customerRel != null && customerRel.TypeFor(User.ENTITY) == RelationType.OneToMany) { var q = new EntityQuery2(User.ENTITY); q.WhereRelated(new RelationQuery(em.Name, customerRel.Role, attach.Id.Value)); var cust = _repository.Read(q); if (cust != null) usersToAttach.Add(cust.Id); } } foreach (var id in usersToAttach) { update.Attach(User.ENTITY, Payment.ROLE_CUSTOMER, id); } } } }
private void AppyLogicBefore(EntityOperation operation, EntityOperationContext context) { foreach (var logic in _logics) logic.Before(operation, context); }
public void After(EntityOperation operation, EntityOperationContext context, EntityOperationResult result) { if (!result.Success) return; }
public void Before(Core.Services.tmp.EntityOperation operation, EntityOperationContext context) { if (operation is EntityUpdate) { var update = operation as EntityUpdate; if (!update.Id.HasValue) update.Set("CreatedOn", DateTime.Now); } }
public void Before(Services.tmp.EntityOperation operation, EntityOperationContext context) { if (operation.IsEntity(NbuLibrary.Core.Domain.File.ENTITY) && operation is EntityUpdate) { var update = operation as EntityUpdate; if (update.IsCreate()) { var access = update.Attach(NbuLibrary.Core.Domain.User.ENTITY, Roles.Access, _securityService.CurrentUser.Id); access.Set("Type", FileAccessType.Owner); } else { var accessUpdates = update.GetMultipleRelationUpdates(User.ENTITY, Roles.Access); if (accessUpdates != null) foreach (var ac in accessUpdates) { if (ac.Operation == RelationOperation.Detach) continue; if (ac.ContainsProperty("Type") && ac.Get<FileAccessType>("Type") == FileAccessType.Token) ac.Set("Token", Guid.NewGuid());//TODO: token based file access } } } else if (operation.IsEntity(NbuLibrary.Core.Domain.File.ENTITY) && operation is EntityDelete) { var file = _fileService.GetFile(operation.Id.Value); context.Set<NbuLibrary.Core.Domain.File>(CTXKEY_FILEDELETION, file); } }
public void After(EntityOperation operation, EntityOperationContext context, EntityOperationResult result) { if (!result.Success) return; if (!operation.IsEntity(User.ENTITY)) return; var update = operation as EntityUpdate; if (update == null) return; if (context.Get<bool>(CTXKEY_USER_PASSWORDRECOVERY)) { var user = new User(_repository.Read(new EntityQuery2(update.Entity, update.Id.Value) { AllProperties = true })); var template = _templateService.Get(new Guid(NotificationTemplates.USER_PASSWORDRECOVERY)); string subject = null, body = null; Dictionary<string, Entity> templateContext = new Dictionary<string, Entity>(StringComparer.InvariantCultureIgnoreCase); templateContext.Add("User", user); _templateService.Render(template, templateContext, out subject, out body); //TODO: async execution _notificationService.SendEmail(user.Email, subject, body, null); } if (context.Get<bool>(CTXKEY_USER_CREATION)) { var user = new User(_repository.Read(new EntityQuery2(update.Entity, update.Id.Value) { AllProperties = true })); var pwd = context.Get<string>(CTXKEY_PASSWORD_UPDATE); user.SetData<String>("Password", pwd); var template = _templateService.Get(new Guid(NotificationTemplates.USER_CREATED)); string subject = null, body = null; Dictionary<string, Entity> templateContext = new Dictionary<string, Entity>(StringComparer.InvariantCultureIgnoreCase); templateContext.Add("User", user); _templateService.Render(template, templateContext, out subject, out body); //TODO: async execution _notificationService.SendEmail(user.Email, subject, body, null); if (update.ContainsProperty("IsActive") && update.Get<bool>("IsActive")) SendUserActivationEmail(user); } else if (update.ContainsProperty("IsActive") && update.Get<bool>("IsActive")) { var user = new User(_repository.Read(new EntityQuery2(update.Entity, update.Id.Value) { AllProperties = true })); SendUserActivationEmail(user); } else if (context.Get<int>(CTXKEY_UPDATE_PROFILE) > 0) { if (update.ContainsRelation("UserGroup", "UserGroup") || update.ContainsProperty("FacultyNumber") || update.ContainsProperty("CardNumber")) { var user = new User(context.Get<int>(CTXKEY_UPDATE_PROFILE)); user.IsActive = false; _repository.Update(user); _securityService.Logout(); result.Data.Add("account_warning_logged_out", true); } if (update.ContainsProperty("Email")) { _securityService.UpdateCurrentUserEmail(update.Get<string>("Email")); result.Data.Add("account_event_email_changed", update.Get<string>("Email")); } } }
public void After(Services.tmp.EntityOperation operation, EntityOperationContext context, EntityOperationResult result) { if (operation.IsEntity(NbuLibrary.Core.Domain.File.ENTITY) && operation is EntityDelete && result.Success) { var file = context.Get<NbuLibrary.Core.Domain.File>(CTXKEY_FILEDELETION); if (file != null) _fileService.DeleteFileContent(Guid.Parse(file.ContentPath)); } }