Exemplo n.º 1
0
        public JsonResult GetRoles(string completeActionName)
        {
            AcaoRepository acaoRep   = new AcaoRepository(db);
            Acao           existente = acaoRep.GetByName(completeActionName);

            if (existente == null)
            {
                existente      = new Acao();
                existente.Nome = completeActionName;

                try
                {
                    Type     tipo = typeof(TPAController);
                    Assembly asm  = Assembly.GetAssembly(tipo);
                    SegurancaActionViewModel action =
                        (
                            from t in asm.GetTypes().SelectMany(tp => tp.GetMethods(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public), (parent, child) => new { TipoController = parent, MetodoAction = child })
                            where tipo.IsAssignableFrom(t.TipoController) && ((t.TipoController.Name + "/" + t.MetodoAction.Name) == completeActionName)
                            orderby t.MetodoAction.Name
                            select new SegurancaActionViewModel
                    {
                        NomeAction = t.TipoController.Name + "/" + t.MetodoAction.Name,
                        Nome = ((t.MetodoAction.GetCustomAttribute(typeof(TPADescricaoAcaoControllerAttribute), false) as TPADescricaoAcaoControllerAttribute) ?? new TPADescricaoAcaoControllerAttribute(t.MetodoAction.Name, "")).Nome,
                        Descricao = ((t.MetodoAction.GetCustomAttribute(typeof(TPADescricaoAcaoControllerAttribute), false) as TPADescricaoAcaoControllerAttribute) ?? new TPADescricaoAcaoControllerAttribute("", t.TipoController.Name + "/" + t.MetodoAction.Name)).Descricao,
                    }
                        ).FirstOrDefault <SegurancaActionViewModel>();

                    if (action != null)
                    {
                        existente.NomeAmigavel      = action.Nome;
                        existente.DescricaoAmigavel = action.Descricao;
                    }
                }
                catch
                {
                }

                acaoRep.Save(existente);
            }

            if (existente.Perfis != null && existente.Perfis.Count > 0)
            {
                return(Json(existente.Perfis.Select(x => x.Id).ToArray <int>()));
            }
            else
            {
                return(Json(null));
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// obtém do assembly os novos controllers/actions para cadastrar na tabela Acao
        /// </summary>
        /// <param name="tipo">Type - tipo de onde vão ser retiradas as actions, geralmente um controller</param>
        /// <returns>int - O número de ações cadastradas</returns>
        public int ImportarDoAssembly(Type tipo, bool atualizarDescricoes = false)
        {
            int result = 0;

            Assembly asm = Assembly.GetAssembly(tipo);

            AcaoRepository rep = new AcaoRepository(this._db);


            ///obtém a lista de actions do assembly
            ///http://stackoverflow.com/questions/21583278/getting-all-controllers-and-actions-names-in-c-sharp
            List <SegurancaActionViewModel> actionList =
                (
                    from t in asm.GetTypes().SelectMany(tp => tp.GetMethods(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public), (parent, child) => new { TipoController = parent, MetodoAction = child })
                    where tipo.IsAssignableFrom(t.TipoController)
                    orderby t.MetodoAction.Name
                    select new SegurancaActionViewModel
            {
                NomeAction = t.TipoController.Name + "/" + t.MetodoAction.Name,
                Nome = ((t.MetodoAction.GetCustomAttribute(typeof(TPADescricaoAcaoControllerAttribute), false) as TPADescricaoAcaoControllerAttribute) ?? new TPADescricaoAcaoControllerAttribute(t.MetodoAction.Name, "")).Nome,
                Descricao = ((t.MetodoAction.GetCustomAttribute(typeof(TPADescricaoAcaoControllerAttribute), false) as TPADescricaoAcaoControllerAttribute) ?? new TPADescricaoAcaoControllerAttribute("", t.TipoController.Name + "/" + t.MetodoAction.Name)).Descricao,
            }
                ).ToList <SegurancaActionViewModel>();



            foreach (SegurancaActionViewModel a in actionList)
            {
                Acao acao = rep.GetByName(a.NomeAction);
                if (acao == null)
                {
                    acao                   = new Acao();
                    acao.Nome              = a.NomeAction;
                    acao.NomeAmigavel      = a.Nome;
                    acao.DescricaoAmigavel = a.Descricao;
                    rep.Save(acao);
                }
                else if (atualizarDescricoes)
                {
                    acao.NomeAmigavel      = a.Nome;
                    acao.DescricaoAmigavel = a.Descricao;
                    rep.Save(acao);
                }


                result++;
            }


            //obtém a lista de acoes do SegurancaResources
            ResourceSet   resourceSet    = SegurancaResources.ResourceManager.GetResourceSet(CultureInfo.CurrentCulture, true, true);
            List <string> listaResources = new List <string>();

            foreach (DictionaryEntry entry in resourceSet)
            {
                listaResources.Add(entry.Value.ToString());
            }

            var ResourcesParaCadastrar = listaResources.Where(x => !this._db.Acoes.Select(a => a.Nome).Any(c => c == x)).ToList <string>();

            foreach (string s in ResourcesParaCadastrar)
            {
                this._db.Acoes.Add(new Acao
                {
                    Nome = s
                });

                result++;
            }


            this._db.SaveChanges();

            return(result);
        }