Exemplo n.º 1
0
        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");
            }
        }
Exemplo n.º 2
0
        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"));
                    }
                }
            }
        }
Exemplo n.º 3
0
        private string InstallModulesPermissions(IModule module)
        {
            StringBuilder info = new StringBuilder();

            info.Append("<ul>");
            ModulePermission mp = new ModulePermission()
            {
                Available = module.Requirements.Permissions != null?module.Requirements.Permissions.ToArray() : new string[0],
                                ModuleID   = module.Id,
                                ModuleName = module.Name
            };
            var q = new EntityQuery2(ModulePermission.ENTITY);

            q.AddProperty("Available");
            q.WhereIs("moduleId", module.Id);
            using (var dbContext = _dbService.GetDatabaseContext(true))
            {
                var ex = _repository.Read(q);
                if (ex == null)
                {
                    _repository.Create(mp.Entity);
                    foreach (var p in mp.Available)
                    {
                        info.AppendFormat("<li>{0} - added.</li>", p);
                    }
                }
                else if (ex.GetData <string>("Available") != mp.Entity.GetData <string>("Available"))
                {
                    var      oldRaw = ex.GetData <string>("Available");
                    string[] old    = null;
                    if (string.IsNullOrEmpty(oldRaw))
                    {
                        old = new string[0];
                    }
                    else
                    {
                        old = oldRaw.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                    }
                    ex.SetData <string>("Available", mp.Entity.GetData <string>("Available"));
                    _repository.Update(ex);
                    foreach (var p in mp.Available)
                    {
                        if (!old.Contains(p))
                        {
                            info.AppendFormat("<li>{0} - added.</li>", p);
                        }
                    }
                    foreach (var p in old)
                    {
                        if (!mp.Available.Contains(p))
                        {
                            info.AppendFormat("<li>{0} - removed.</li>", p);
                        }
                    }
                }
                dbContext.Complete();
            }
            info.Append("</ul>");
            return(info.ToString());
        }
        public InspectionResult Inspect(EntityOperation operation)
        {
            if (operation.IsEntity(User.ENTITY))
            {
                if (_securityService.HasModulePermission(_securityService.CurrentUser, AccountModule.Id, Permissions.UserManagement))
                    return InspectionResult.Allow;

                if(operation is EntityUpdate)
                {
                    var update = operation as EntityUpdate;
                    if (update.ContainsProperty("RecoveryCode"))
                        return InspectionResult.Deny; //Only users with UserManagement permission can access this property
                }

                if (operation.Id.HasValue && _securityService.HasModulePermission(_securityService.CurrentUser, AccountModule.Id, Permissions.UserActivation))
                {
                    EntityQuery2 query = new EntityQuery2("User", operation.Id.Value);
                    query.AddProperty("isActive");
                    var e = _repository.Read(query);
                    if (e.GetData<bool>("IsActive") == false)
                        return InspectionResult.Allow;
                }
                else if (operation is EntityUpdate)
                {
                    var update = operation as EntityUpdate;
                    if (update.Id.HasValue
                        && update.Id.Value == _securityService.CurrentUser.Id
                        && !update.ContainsProperty("IsActive"))//TODO: allowing the user to edit his data and relations
                        return InspectionResult.Allow;
                }
            }

            return InspectionResult.None;
        }
        public InspectionResult InspectQuery(EntityQuery2 query)
        {
            if (query.IsForEntity(User.ENTITY))
            {
                if (_securityService.HasModulePermission(_securityService.CurrentUser, AccountModule.Id, Permissions.UserManagement))
                {
                    return(InspectionResult.Allow);
                }
                else if (query.GetRuleByProperty("Id") != null && Convert.ToInt32(query.GetRuleByProperty("Id").Values[0]) == _securityService.CurrentUser.Id)
                {
                    return(InspectionResult.Allow);
                }

                if (query.HasProperty("RecoveryCode"))
                {
                    return(InspectionResult.Deny); //Only users with UserManagement permission can access this property
                }
                bool hasUserActivationPermission = _securityService.HasModulePermission(_securityService.CurrentUser, AccountModule.Id, Permissions.UserActivation);
                var  isActiveRule = query.Rules.Find(r => r.IsForProperty("IsActive"));
                if (isActiveRule != null &&
                    isActiveRule.Values.Count() == 1 &&
                    Convert.ToBoolean(isActiveRule.Values.Single()) == false &&
                    hasUserActivationPermission)
                {
                    return(InspectionResult.Allow);
                }

                if (hasUserActivationPermission && !query.HasProperty("IsActive"))
                {
                    query.AddProperty("IsActive");
                }
            }
            else if (query.IsForEntity(UserGroup.ENTITY))
            {
                if (_securityService.CurrentUser.UserType == UserTypes.Librarian ||
                    _securityService.HasModulePermission(_securityService.CurrentUser, AccountModule.Id, Permissions.UserGroupManagement) ||
                    _securityService.HasModulePermission(_securityService.CurrentUser, AccountModule.Id, Permissions.UserActivation))
                {
                    return(InspectionResult.Allow);
                }
                else if (query.GetRuleByProperty("UserType") != null &&
                         (UserTypes)Convert.ToInt32(query.GetRuleByProperty("UserType").Values[0]) == _securityService.CurrentUser.UserType &&
                         !query.HasInclude("User", "UserGroup"))
                {
                    return(InspectionResult.Allow);
                }
            }

            return(InspectionResult.None);
        }
Exemplo n.º 6
0
        public InspectionResult Inspect(EntityOperation operation)
        {
            if (operation.IsEntity(User.ENTITY))
            {
                if (_securityService.HasModulePermission(_securityService.CurrentUser, AccountModule.Id, Permissions.UserManagement))
                {
                    return(InspectionResult.Allow);
                }

                if (operation is EntityUpdate)
                {
                    var update = operation as EntityUpdate;
                    if (update.ContainsProperty("RecoveryCode"))
                    {
                        return(InspectionResult.Deny); //Only users with UserManagement permission can access this property
                    }
                }


                if (operation.Id.HasValue && _securityService.HasModulePermission(_securityService.CurrentUser, AccountModule.Id, Permissions.UserActivation))
                {
                    EntityQuery2 query = new EntityQuery2("User", operation.Id.Value);
                    query.AddProperty("isActive");
                    var e = _repository.Read(query);
                    if (e.GetData <bool>("IsActive") == false)
                    {
                        return(InspectionResult.Allow);
                    }
                }
                else if (operation is EntityUpdate)
                {
                    var update = operation as EntityUpdate;
                    if (update.Id.HasValue &&
                        update.Id.Value == _securityService.CurrentUser.Id &&
                        !update.ContainsProperty("IsActive"))   //TODO: allowing the user to edit his data and relations
                    {
                        return(InspectionResult.Allow);
                    }
                }
            }

            return(InspectionResult.None);
        }
        public InspectionResult InspectQuery(EntityQuery2 query)
        {
            if (query.IsForEntity(User.ENTITY))
            {
                if (_securityService.HasModulePermission(_securityService.CurrentUser, AccountModule.Id, Permissions.UserManagement))
                    return InspectionResult.Allow;
                else if (query.GetRuleByProperty("Id") != null && Convert.ToInt32(query.GetRuleByProperty("Id").Values[0]) == _securityService.CurrentUser.Id)
                    return InspectionResult.Allow;

                if (query.HasProperty("RecoveryCode"))
                    return InspectionResult.Deny; //Only users with UserManagement permission can access this property

                bool hasUserActivationPermission = _securityService.HasModulePermission(_securityService.CurrentUser, AccountModule.Id, Permissions.UserActivation);
                var isActiveRule = query.Rules.Find(r => r.IsForProperty("IsActive"));
                if (isActiveRule != null
                    && isActiveRule.Values.Count() == 1
                    && Convert.ToBoolean(isActiveRule.Values.Single()) == false
                    && hasUserActivationPermission)
                    return InspectionResult.Allow;

                if (hasUserActivationPermission && !query.HasProperty("IsActive"))
                    query.AddProperty("IsActive");
            }
            else if (query.IsForEntity(UserGroup.ENTITY))
            {
                if (_securityService.CurrentUser.UserType == UserTypes.Librarian
                    || _securityService.HasModulePermission(_securityService.CurrentUser, AccountModule.Id, Permissions.UserGroupManagement)
                    || _securityService.HasModulePermission(_securityService.CurrentUser, AccountModule.Id, Permissions.UserActivation))
                    return InspectionResult.Allow;
                else if (query.GetRuleByProperty("UserType") != null
                    && (UserTypes)Convert.ToInt32(query.GetRuleByProperty("UserType").Values[0]) == _securityService.CurrentUser.UserType
                    && !query.HasInclude("User", "UserGroup"))
                    return InspectionResult.Allow;
            }

            return InspectionResult.None;
        }
Exemplo n.º 8
0
        public void Test_EntityRepo_Sorting()
        {
            var dbService = new TestDatabaseService();
            var repository = new EntityRepository(dms, dbService, new SequenceProvider(dbService));
            using (var ctx = dbService.GetDatabaseContext(true))
            {
                #region prepare data
                var jordan = new Author()
                {
                    FirstName = "Robert",
                    LastName = "Jordan",
                    IsAlive = false,
                    Born = new DateTime(1948, 10, 17),
                    Rating = 10.0m
                };

                var feist = new Author()
                {
                    FirstName = "Raymond",
                    LastName = "Feist",
                    IsAlive = true,
                    Born = new DateTime(1963, 2, 14),
                    Rating = 6.7m
                };

                var fb1 = new Book()
                {
                    Title = "The Apprentice",
                    Price = 19.90m
                };

                var fb2 = new Book()
                {
                    Title = "The Magician",
                    Price = 17.10m
                };

                var jb1 = new Book()
                {
                    Title = "The Shadow is Rising",
                    Price = 21.15m
                };
                var jb2 = new Book()
                {
                    Title = "The Eye of the World",
                    Price = 25.80m
                };

                repository.Create(jordan);
                repository.Create(feist);
                repository.Create(fb1);
                repository.Create(fb2);
                repository.Create(jb1);
                repository.Create(jb2);
                repository.Attach(feist, new Relation("author", fb1));
                repository.Attach(feist, new Relation("author", fb2));
                repository.Attach(jordan, new Relation("author", jb1));
                repository.Attach(jordan, new Relation("author", jb2));
                #endregion

                var query = new EntityQuery2("author");
                query.AddProperties("firstname", "lastname", "born");
                query.SortBy = new Sorting("firstname");
                var res = repository.Search(query);

                Assert.AreEqual(feist.Id, res.First().Id);
                query.SortBy = new Sorting("firstname", true);
                res = repository.Search(query);
                Assert.AreEqual(jordan.Id, res.First().Id);

                var q = new EntityQuery2("book");
                q.AddProperty("title");
                q.WhereAnyOf("id", new object[] { fb1.Id, jb1.Id, jb2.Id });
                q.SortBy = new Sorting("title");
                res = repository.Search(q);
                Assert.AreEqual(fb1.Id, res.ElementAt(0).Id);
                Assert.AreEqual(jb2.Id, res.ElementAt(1).Id);
                Assert.AreEqual(jb1.Id, res.ElementAt(2).Id);

                ////greater then
                //EntityQuery2 q = new EntityQuery2("book");
                //q.WhereGreaterThen("price", 19.0m);
                //Assert.AreEqual(3, repository.Search(q).Count());

                ////less then
                //q = new EntityQuery2("book");
                //q.WhereLessThen("price", 20.0m);
                //Assert.AreEqual(2, repository.Search(q).Count());

                ////is boolean
                //q = new EntityQuery2("author");
                //q.WhereIs("isalive", true);
                //var r = repository.Search(q);
                //Assert.AreEqual(1, r.Count());
                //Assert.AreEqual(feist.Id, r.Single().Id);

                ////is string (ignore case)
                //q = new EntityQuery2("author");
                //q.WhereIs("lastname", "jordan");
                //r = repository.Search(q);
                //Assert.AreEqual(1, r.Count());
                //Assert.AreEqual(jordan.Id, r.Single().Id);

                ////starts with
                //q = new EntityQuery2("author");
                //q.WhereStartsWith("firstname", "ra");
                //r = repository.Search(q);
                //Assert.AreEqual(1, r.Count());
                //Assert.AreEqual(feist.Id, r.Single().Id);

                ////ends with
                //q = new EntityQuery2("book");
                //q.WhereEndsWith("title", "world");
                //r = repository.Search(q);
                //Assert.AreEqual(1, r.Count());
                //Assert.AreEqual(jb2.Id, r.Single().Id);

                ////less then
                //q = new EntityQuery2("book");
                //q.WhereAnyOf("id", new object[] { fb1.Id, jb1.Id, jb2.Id });
                //Assert.AreEqual(3, repository.Search(q).Count());

                ////between decimal
                //q = new EntityQuery2("book");
                //q.WhereBetween("price", 19.0m, 22.0m);
                //Assert.AreEqual(2, repository.Search(q).Count());

                ////between datetime
                //q = new EntityQuery2("author");
                //q.WhereBetween("born", new DateTime(1948, 1, 1), new DateTime(1949, 1, 1));
                //r = repository.Search(q);
                //Assert.AreEqual(1, r.Count());
                //Assert.AreEqual(jordan.Id, r.Single().Id);

                //q = new EntityQuery2("author");
                //q.WhereBetween("born", new DateTime(1948, 1, 1), new DateTime(1949, 1, 1));
                //q.WhereIs("isalive", true);
                //Assert.AreEqual(0, repository.Search(q).Count());

                //q = new EntityQuery2("author");
                //q.WhereBetween("born", new DateTime(1960, 1, 1), new DateTime(1970, 1, 1));
                //q.WhereIs("isalive", true);
                //q.WhereStartsWith("firstname", "ra");
                //Assert.AreEqual(1, repository.Search(q).Count());

                //repository.Complete();
            }
        }
Exemplo n.º 9
0
        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"));
                    }
                }
            }
        }
Exemplo n.º 10
0
        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");
            }
        }
Exemplo n.º 11
0
 private string InstallModulesPermissions(IModule module)
 {
     StringBuilder info = new StringBuilder();
     info.Append("<ul>");
     ModulePermission mp = new ModulePermission()
     {
         Available = module.Requirements.Permissions != null ? module.Requirements.Permissions.ToArray() : new string[0],
         ModuleID = module.Id,
         ModuleName = module.Name
     };
     var q = new EntityQuery2(ModulePermission.ENTITY);
     q.AddProperty("Available");
     q.WhereIs("moduleId", module.Id);
     using (var dbContext = _dbService.GetDatabaseContext(true))
     {
         var ex = _repository.Read(q);
         if (ex == null)
         {
             _repository.Create(mp.Entity);
             foreach (var p in mp.Available)
             {
                 info.AppendFormat("<li>{0} - added.</li>", p);
             }
         }
         else if (ex.GetData<string>("Available") != mp.Entity.GetData<string>("Available"))
         {
             var oldRaw = ex.GetData<string>("Available");
             string[] old = null;
             if (string.IsNullOrEmpty(oldRaw))
                 old = new string[0];
             else
                 old = oldRaw.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
             ex.SetData<string>("Available", mp.Entity.GetData<string>("Available"));
             _repository.Update(ex);
             foreach (var p in mp.Available)
             {
                 if (!old.Contains(p))
                     info.AppendFormat("<li>{0} - added.</li>", p);
             }
             foreach (var p in old)
             {
                 if (!mp.Available.Contains(p))
                     info.AppendFormat("<li>{0} - removed.</li>", p);
             }
         }
         dbContext.Complete();
     }
     info.Append("</ul>");
     return info.ToString();
 }