Пример #1
0
        public async Task <ActionResult> Put(
            int id,
            [FromBody] GroupTrees model,
            [FromServices] DataContext context,
            [FromServices] IUnitOfWork UnitOfWork)
        {
            if (id != model.Id)
            {
                return(BadRequest(new Error("Identificadores diferentes", "Id informada difere do Grupo de Árvores!")));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                context.Entry <GroupTrees>(model).State = EntityState.Modified;
                await UnitOfWork.Commit();
            }
            catch (DbUpdateConcurrencyException)
            {
                return(BadRequest(new Error("Erro ao atualizar o Banco de Dados", "Este Grupo de Árvores já foi atualizado!")));
            }
            catch (Exception)
            {
                return(BadRequest(new Error("Erro ao atualizar o Banco de Dados", "Não foi possível Atualizar o Grupo de Árvores!")));
            }
            return(Ok());
        }
Пример #2
0
        public async Task <ActionResult <IId> > Post(
            [FromBody] GroupTrees model,
            [FromServices] DataContext context,
            [FromServices] IUnitOfWork UnitOfWork)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                context.GroupTrees.Add(model);
                await UnitOfWork.Commit();
            }
            catch (Exception)
            {
                return(BadRequest(new Error("Erro ao gravar no Banco de Dados", "Não foi possível criar o Grupo de Árvores!")));
            }
            return(Ok(model.Id));
        }
Пример #3
0
        public async Task <ActionResult <List <GroupTrees> > > Post(
            [FromBody] GroupTrees model,
            [FromServices] DataContext context
            )
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                context.GroupTrees.Add(model);
                await context.SaveChangesAsync();

                return(Ok(model));
            }
            catch
            {
                return(BadRequest(new { message = "It was no possible to create this tree group" }));
            }
        }
Пример #4
0
        internal static ModulePrivilegeRelationship GetModuleFunctionPrivilege(int userId, int moduleId, GroupTrees userGroupTrees, ISession hibernateSession = null)
        {
            var mySession = hibernateSession ?? NHibernateHelper.CurrentHelper.OpenSession();

            try
            {
                var user            = mySession.Get <User>(userId);
                var privilegeResult = new ModulePrivilegeRelationship()
                {
                    FunctionId = moduleId
                };
                //注意优先级
                //第一级,用户自己的设置
                var query =
                    mySession.CreateQuery("from ModuleFunctionMap where end_date is null and EntityTypeId = " +
                                          EntityStructureApi.GetStructureByTypeName("User")?.Id
                                          + " and EntityId = " + userId + " and ModuleId = " + moduleId);

                var result = query.List <ModuleFunctionMap>().FirstOrDefault();
                if (result != null)
                {
                    privilegeResult.IsGroupLevel = false;
                    privilegeResult.Source       = user.Name;
                    privilegeResult.Status       = result.State;
                    return(privilegeResult);
                }

                privilegeResult.IsGroupLevel = true;
                var groupPrivileges = new Dictionary <int, int>();
                foreach (var leaf in userGroupTrees.Leaves)
                {
                    var  node  = leaf;
                    bool found = false;
                    while (true)
                    {
                        if (found)
                        {
                            groupPrivileges[node.GroupId] = -1;
                        }
                        else
                        {
                            int status;
                            if (!groupPrivileges.TryGetValue(node.GroupId, out status))
                            {
                                query =
                                    mySession.CreateQuery("from ModuleFunctionMap where end_date is null and EntityTypeId = " +
                                                          EntityStructureApi.GetStructureByTypeName("UserGroup")?.Id
                                                          + " and EntityId = ? and ModuleId = " + moduleId);
                                query.SetInt32(0, node.GroupId);
                                result = query.List <ModuleFunctionMap>().FirstOrDefault();
                                if (result != null)
                                {
                                    groupPrivileges.Add(node.GroupId, result.State);
                                    found = true;
                                }
                                else
                                {
                                    groupPrivileges.Add(node.GroupId, -1);
                                }
                            }
                        }
                        if (node.Parent == null)
                        {
                            break;
                        }
                        node = node.Parent;
                    }
                }
                if (groupPrivileges.ContainsValue(SessionManager.PermissionStateFalse))
                {
                    privilegeResult.Status = SessionManager.PermissionStateFalse;
                    privilegeResult.Source = mySession.Get <UserGroup>(
                        groupPrivileges.First(gp => gp.Value == SessionManager.PermissionStateFalse).Key).Name;
                    return(privilegeResult);
                }
                else if (groupPrivileges.ContainsValue(SessionManager.PermissionStateTrue))
                {
                    privilegeResult.Status = SessionManager.PermissionStateTrue;
                    privilegeResult.Source = mySession.Get <UserGroup>(
                        groupPrivileges.First(gp => gp.Value == SessionManager.PermissionStateTrue).Key).Name;
                    return(privilegeResult);
                }
                return(null);
            }
            finally
            {
                if (hibernateSession == null)
                {
                    mySession.Dispose();
                }
            }
        }