Пример #1
0
        private static PermissionContract GetPermissionContract(UM_Resource resource, Guid groupID, Guid projectID)
        {
            var session = Hb8Factory.InitSession();

            var permission = (from n in session.Query <UM_Permission>()
                              where n.DateDeleted == null &&
                              n.GroupID == groupID &&
                              n.ResourceID == resource.ID
                              orderby n.RuleValue descending
                              select n).FirstOrDefault();

            if (permission != null)
            {
                return(permission.ToContract(projectID));
            }

            if (resource.Parent == null)
            {
                var newPemission = new UM_Permission
                {
                    ID          = Guid.NewGuid(),
                    GroupID     = groupID,
                    ResourceID  = resource.ID,
                    RuleValue   = (int)RulePermissionsEnum.View,
                    DateCreated = DateTime.Now
                };

                session.SubmitInsert(newPemission);

                return(newPemission.ToContract(projectID));
            }

            return(GetPermissionContract(resource.Parent, groupID, projectID));
        }
Пример #2
0
        private static PermissionContract GetPermissionContract(UM_Resource resource, Guid groupID, Guid projectID)
        {
            using (var db = DcFactory.Create <UserManagementDataContext>())
            {
                var permission = (from n in db.UM_Permissions
                                  where n.DateDeleted == null &&
                                  n.GroupID == groupID &&
                                  n.ResourceID == resource.ID
                                  orderby n.RuleValue descending
                                  select n).FirstOrDefault();

                if (permission != null)
                {
                    return(permission.ToContract(projectID));
                }

                if (resource.Parent == null)
                {
                    var newPemission = new UM_Permission();

                    newPemission.ID          = Guid.NewGuid();
                    newPemission.GroupID     = groupID;
                    newPemission.ResourceID  = resource.ID;
                    newPemission.RuleValue   = (int)RulePermissionsEnum.View;
                    newPemission.DateCreated = DateTime.Now;

                    db.UM_Permissions.InsertOnSubmit(newPemission);
                    db.SubmitChanges();

                    return(newPemission.ToContract(projectID));
                }

                return(GetPermissionContract(resource.Parent, groupID, projectID));
            }
        }
Пример #3
0
        private static PermissionContract GetPermissionContract(UM_Resource resource, IList <Guid> groupsID, Guid projectID)
        {
            var result = new PermissionContract
            {
                ProjectID    = projectID,
                ResourceID   = resource.ID,
                ResourcePath = resource.FullPath()
            };

            foreach (var groupID in groupsID)
            {
                var groupPerm = GetPermissionContract(resource, groupID, projectID);
                result.RuleValue |= groupPerm.RuleValue;

                if (groupPerm.PermissionParameter != null)
                {
                    foreach (var pair in groupPerm.PermissionParameter)
                    {
                        if (result.PermissionParameter.ContainsKey(pair.Key))
                        {
                            var key = $"{pair.Key}_{groupID}";
                            result.PermissionParameter[key] = pair.Value;
                        }
                        else
                        {
                            result.PermissionParameter[pair.Key] = pair.Value;
                        }
                    }
                }
            }

            return(result);
        }
Пример #4
0
        protected void btnOK_Click(object sender, EventArgs e)
        {
            var model = resourceControl.Model;

            if (String.IsNullOrWhiteSpace(model.Name))
            {
                lblErrorMessage.Text = "შეიყვანეთ სახელი";

                mpeResource.Show();
                return;
            }

            var converter = new ResourceModelEntityConverter(HbSession);

            var entity = HbSession.Query <UM_Resource>().FirstOrDefault(n => n.ID == model.ID);

            if (entity == null)
            {
                entity = new UM_Resource
                {
                    ID          = Guid.NewGuid(),
                    DateCreated = DateTime.Now
                };
            }

            converter.FillObject(entity, model);

            HbSession.SubmitChanges(entity);

            FillResources();

            mpeResource.Hide();
        }
Пример #5
0
    protected void btResourceOK_Click(object sender, CommandEventArgs e)
    {
        UM_Resource item;

        var resourceID = DataConverter.ToNullableGuid(hdResourceID.Value);

        if (resourceID != null)
        {
            item = DataContext.UM_Resources.First(n => n.ID == resourceID);
        }
        else
        {
            item = new UM_Resource
            {
                ID          = Guid.NewGuid(),
                DateCreated = DateTime.Now
            };

            DataContext.UM_Resources.InsertOnSubmit(item);
        }

        item.ProjectID = DataConverter.ToGuid(cmbProject.SelectedItem.Value);
        item.ParentID  = DataConverter.ToNullableGuid(e.CommandArgument);

        item.Name  = tbResourceName.Text;
        item.Type  = Int32.Parse(cmbResourceType.SelectedItem.Value.ToString());
        item.Value = tbResourceValue.Text;

        DataContext.SubmitChanges();

        FillResources();
    }
Пример #6
0
 protected IEnumerable <UM_Resource> GetAllParents(UM_Resource resource, IDictionary <Guid, UM_Resource> allResourceses)
 {
     while (resource != null)
     {
         resource = allResourceses.GetValueOrDefault(resource.ParentID.GetValueOrDefault());
         if (resource != null)
         {
             yield return(resource);
         }
     }
 }
Пример #7
0
        protected IEnumerable <UM_Resource> GetAllChildren(UM_Resource resource, ILookup <Guid, UM_Resource> allResourceses)
        {
            var children = allResourceses[resource.ID];

            var stack = new Stack <UM_Resource>(children);

            while (stack.Count > 0)
            {
                var item = stack.Pop();
                yield return(item);
            }
        }
Пример #8
0
        public static String FullPath(this UM_Resource resource)
        {
            var list = new List <String>();

            var parent = resource;

            while (parent != null)
            {
                list.Add(parent.Value);
                parent = parent.Parent;
            }

            list.Reverse();

            return(String.Join("/", list));
        }
Пример #9
0
        public static ResourceContract ToContract(this UM_Resource entity)
        {
            if (entity == null)
            {
                return(null);
            }

            var contract = new ResourceContract();

            contract.DateChanged = entity.DateChanged;
            contract.DateCreated = entity.DateCreated;
            contract.Description = entity.Description;
            contract.ID          = entity.ID;
            contract.Name        = entity.Name;
            contract.Type        = (RuleTypesEnum)entity.Type;
            contract.ParentID    = entity.ParentID;
            contract.Value       = entity.Value;
            contract.ProjectID   = entity.ProjectID;
            return(contract);
        }
Пример #10
0
        public static UM_Resource ToEntity(this ResourceContract contract)
        {
            if (contract == null)
            {
                return(null);
            }

            var entity = new UM_Resource();

            entity.DateChanged = contract.DateChanged;
            entity.DateCreated = contract.DateCreated;
            entity.DateDeleted = contract.DateDeleted;
            entity.Description = contract.Description;
            entity.ID          = contract.ID;
            entity.Type        = Convert.ToInt32(contract.Type);
            entity.Name        = contract.Name;
            entity.ParentID    = contract.ParentID;
            entity.Value       = contract.Value;
            entity.ProjectID   = contract.ProjectID;

            return(entity);
        }
Пример #11
0
        public static PermissionContract GetResourcePermission(Guid token, String resourcePath)
        {
            var session = Hb8Factory.InitSession();

            var comparer = StringComparer.OrdinalIgnoreCase;

            var pathArray = resourcePath.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

            if (pathArray.Length == 0)
            {
                return(null);
            }

            var projectID = DataConverter.ToNullableGuid(pathArray[0]);

            if (projectID == null)
            {
                return(null);
            }

            var exists = session.Query <UM_Project>().Any(n => n.ID == projectID && n.IsActive);

            if (!exists)
            {
                return(null);
            }

            var user = GetCurrentUser(token);

            if (user == null)
            {
                return(null);
            }

            var requestGlobal = comparer.Equals(pathArray[1], "Global");

            var actualProject = (requestGlobal ? null : projectID);
            var index         = (requestGlobal ? 2 : 1);

            if (pathArray.Length == index)
            {
                return(new PermissionContract());
            }

            var groupsID = (from n in session.Query <UM_GroupUser>()
                            where n.UserID == user.ID &&
                            n.DateDeleted == null
                            let m = n.Group
                                    where m.DateDeleted == null &&
                                    m.ProjectID == projectID
                                    select m.ID).ToList();

            if (groupsID.Count == 0)
            {
                return(null);
            }

            var resQuery = from n in session.Query <UM_Resource>()
                           where n.DateDeleted == null
                           select n;

            if (requestGlobal)
            {
                resQuery = from n in resQuery
                           where n.ProjectID == null
                           select n;
            }
            else
            {
                resQuery = from n in resQuery
                           where n.ProjectID == projectID
                           select n;
            }

            using (var transaction = session.BeginTransaction())
            {
                UM_Resource parentRes  = null;
                UM_Resource currentRes = null;

                for (int i = index; i < pathArray.Length; i++)
                {
                    var resourceName = pathArray[i];

                    var finalQuery = from n in resQuery
                                     where n.Value == resourceName
                                     select n;

                    if (parentRes == null)
                    {
                        finalQuery = from n in finalQuery
                                     where n.ParentID == null
                                     select n;
                    }
                    else
                    {
                        finalQuery = from n in finalQuery
                                     where n.ParentID == parentRes.ID
                                     select n;
                    }

                    currentRes = finalQuery.FirstOrDefault();

                    if (currentRes == null)
                    {
                        currentRes = new UM_Resource
                        {
                            ID          = Guid.NewGuid(),
                            DateCreated = DateTime.Now,
                            Name        = resourceName,
                            ProjectID   = actualProject,
                            Description = "Automatic Record",
                            Value       = resourceName,
                            Type        = 0
                        };

                        if (parentRes != null)
                        {
                            currentRes.ParentID = parentRes.ID;
                        }

                        session.Save(currentRes);
                    }

                    parentRes = currentRes;
                }

                transaction.Commit();

                return(GetPermissionContract(currentRes, groupsID, projectID.Value));
            }
        }
Пример #12
0
        public static PermissionContract GetResourcePermission(Guid token, String resourcePath)
        {
            using (var db = DcFactory.Create <UserManagementDataContext>())
            {
                var comparer = StringComparer.OrdinalIgnoreCase;

                var pathArray = resourcePath.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                if (pathArray.Length == 0)
                {
                    return(null);
                }

                var projectID = DataConverter.ToNullableGuid(pathArray[0]);
                if (projectID == null)
                {
                    return(null);
                }

                var exists = db.UM_Projects.Any(n => n.ID == projectID && n.IsActive == true);
                if (!exists)
                {
                    return(null);
                }

                var user = GetCurrentUser(token);
                if (user == null)
                {
                    return(null);
                }

                var requestGlobal = comparer.Equals(pathArray[1], "Global");

                var actualProject = (requestGlobal ? null : projectID);
                var index         = (requestGlobal ? 1 : 2);

                if (pathArray.Length == index)
                {
                    return(new PermissionContract());
                }

                var groupsID = (from n in db.UM_GroupUsers
                                where n.UserID == user.ID &&
                                n.DateDeleted == null
                                let m = n.Group
                                        where m.DateDeleted == null &&
                                        m.ProjectID == projectID
                                        select m.ID).ToList();

                if (groupsID.Count == 0)
                {
                    return(null);
                }

                var resQuery = from n in db.UM_Resources
                               where n.DateDeleted == null
                               select n;

                if (requestGlobal)
                {
                    resQuery = from n in resQuery
                               where n.ProjectID == null
                               select n;
                }
                else
                {
                    resQuery = from n in resQuery
                               where n.ProjectID == projectID
                               select n;
                }

                UM_Resource parentRes  = null;
                UM_Resource currentRes = null;

                for (int i = index; i < pathArray.Length; i++)
                {
                    var resourceName = pathArray[i];

                    var finalQuery = from n in resQuery
                                     where n.Value == resourceName
                                     select n;

                    if (parentRes == null)
                    {
                        finalQuery = from n in finalQuery
                                     where n.ParentID == null
                                     select n;
                    }
                    else
                    {
                        finalQuery = from n in finalQuery
                                     where n.ParentID == parentRes.ID
                                     select n;
                    }

                    currentRes = finalQuery.FirstOrDefault();

                    if (currentRes == null)
                    {
                        currentRes = new UM_Resource
                        {
                            ID          = Guid.NewGuid(),
                            DateCreated = DateTime.Now,
                            Name        = resourceName,
                            ProjectID   = actualProject,
                            Description = "Automatic Record",
                            Value       = resourceName,
                            Type        = 0
                        };

                        if (parentRes == null)
                        {
                            db.UM_Resources.InsertOnSubmit(currentRes);
                        }
                        else
                        {
                            parentRes.Children.Add(currentRes);
                        }
                    }

                    parentRes = currentRes;
                }

                db.SubmitChanges();

                return(GetPermissionContract(currentRes, groupsID, projectID.Value));
            }
        }