コード例 #1
0
 private static SecurityDescription GetTypeSecurityDescription(Type type)
 {
     if (!TypeSecurityDescription.ContainsKey(type))
     {
         SecurityDescription attribute = (SecurityDescription)type.GetCustomAttribute(typeof(SecurityDescription), true);
         TypeSecurityDescription.Add(type, attribute);
     }
     return(TypeSecurityDescription[type]);
 }
コード例 #2
0
        public static bool hasPermission(object value, string method = null, bool returnException = true, params object[] parameters)
        {
            if (value == null)
            {
                return(false);
            }

            bool result   = true;
            bool isLogued = Security.usuarioLogueado != null;

            Type objectType = (value is Type) ? (Type)value : value.GetType();

            SecurityDescription attribute = GetTypeSecurityDescription(objectType);

            if (attribute != null)
            {
                if (attribute.Estado == SecurityDescription.SeguridadEstado.Activo)
                {
                    result = result && _hasPermission(value, null, returnException);
                }
                else if (attribute.Estado == SecurityDescription.SeguridadEstado.SoloLogueo && isLogued == false)
                {
                    result = false;
                }

                if (result && method != null)
                {
                    attribute = GetTypeMethodSecurityDescription(objectType, method);
                    if (attribute != null)
                    {
                        if (attribute.Estado == SecurityDescription.SeguridadEstado.Activo)
                        {
                            result = result && _hasPermission(value, method, returnException);
                        }
                        else if (attribute.Estado == SecurityDescription.SeguridadEstado.SoloLogueo && isLogued == false)
                        {
                            result = false;
                        }
                    }
                    else
                    {
                        result = false;
                    }
                }
            }
            else
            {
                result = false;
            }

            if (result == false && returnException)
            {
                throw new SecurityException(value, method);
            }

            return(result);
        }
コード例 #3
0
        private static SecurityDescription GetTypeMethodSecurityDescription(Type type, string method)
        {
            if (!TypeMethodSecurityDescription.ContainsKey(type))
            {
                TypeMethodSecurityDescription.Add(type, new Dictionary <string, SecurityDescription>());
            }
            if (!TypeMethodSecurityDescription[type].ContainsKey(method))
            {
                SecurityDescription attribute = null;
                var methodFind = ClassUtils.GetMethod(type, method);
                if (methodFind.Length > 0)
                {
                    attribute = (SecurityDescription)methodFind[0].GetCustomAttribute(typeof(SecurityDescription), true);
                }
                TypeMethodSecurityDescription[type].Add(method, attribute);
            }

            return(TypeMethodSecurityDescription[type][method]);
        }
コード例 #4
0
        public static void CreateAccessSecurity(IApplicationBuilder app)
        {
            using (var scope = app.ApplicationServices.GetRequiredService <IServiceScopeFactory>().CreateScope())
            {
                Assembly assembly = typeof(Security).Assembly;

                SecurityDescription attribute     = null;
                MenuDescription     menuAttribute = null;

                Dictionary <MenuDescription, object> menues = new Dictionary <MenuDescription, object>();
                ArrayList items = new ArrayList();

                GQ_AccesosDto itemClass  = null;
                GQ_AccesosDto itemMethod = null;

                ///
                ///
                /// CREACION DE ROLES
                ///
                ///

                if (ROLES.Length > 0)
                {
                    for (int i = 0; i < ROLES.Length; i++)
                    {
                        if (roles.ContainsKey(ROLES[i]) == false)
                        {
                            roles.Add(ROLES[i], null);
                        }
                    }
                }

                var RolColl = MySQLService.Instance.GetSession <GQ_Perfiles>().findBy(x => true).ToList();

                for (int i = 0; i < roles.Keys.Count; i++)
                {
                    string key = roles.Keys.ElementAt(i);
                    var    rol = RolColl.Where(x => x.KeyName == key).FirstOrDefault();

                    if (rol == null)
                    {
                        rol            = new GQ_Perfiles();
                        rol.Nombre     = key;
                        rol.KeyName    = key;
                        rol.Estado     = Constantes.ESTADO_ACTIVO;
                        rol.Creado     = DateTime.Now;
                        rol.Modificado = DateTime.Now;

                        MySQLService.Instance.GetSession <GQ_Perfiles>().Insert(rol);
                    }

                    roles[key] = new GQ_PerfilesDto(rol);
                }

                ///
                ///
                /// CREACION DE ACCESOS
                ///
                ///

                var elementsType = (from iface in assembly.GetTypes()
                                    where iface.Namespace != null && iface.Namespace.Contains("Controller")
                                    select iface);

                foreach (Type item in elementsType)
                {
                    menuAttribute = (MenuDescription)item.GetCustomAttribute(typeof(MenuDescription), true);
                    if (menuAttribute != null)
                    {
                        menues.Add(menuAttribute, item);
                    }
                    attribute = (SecurityDescription)item.GetCustomAttribute(typeof(SecurityDescription), true);

                    if (attribute != null)
                    {
                        itemClass           = new GQ_AccesosDto();
                        itemClass.ClassName = getNameObject(item);

                        if (attribute.Estado == SecurityDescription.SeguridadEstado.Activo)
                        {
                            itemClass.MethodName = null;
                            itemClass.Nombre     = attribute.Name;
                            itemClass.extra      = attribute;

                            //obtenerProfiles(attribute, roles);

                            items.Add(itemClass);
                        }

                        foreach (MethodInfo method in item.GetMethods())
                        {
                            menuAttribute = (MenuDescription)method.GetCustomAttribute(typeof(MenuDescription), true);
                            if (menuAttribute != null)
                            {
                                menues.Add(menuAttribute, method);
                            }

                            attribute = (SecurityDescription)method.GetCustomAttribute(typeof(SecurityDescription), true);
                            if (attribute != null && attribute.Estado == SecurityDescription.SeguridadEstado.Activo)
                            {
                                itemMethod            = new GQ_AccesosDto();
                                itemMethod.ClassName  = itemClass.ClassName;
                                itemMethod.MethodName = method.Name;
                                itemMethod.Nombre     = attribute.Name;
                                itemMethod.extra      = attribute;

                                //obtenerProfiles(attribute, roles);

                                items.Add(itemMethod);
                            }
                        }
                    }
                }

                var AccesoColl = MySQLService.Instance.GetSession <GQ_Accesos>().findBy(x => true).ToList();

                foreach (GQ_AccesosDto ii in items)
                {
                    var data = AccesoColl.Where(x => x.ClassName == ii.ClassName && x.MethodName == ii.MethodName).FirstOrDefault();
                    if (data == null)
                    {
                        data            = new GQ_Accesos();
                        data.ClassName  = ii.ClassName;
                        data.MethodName = ii.MethodName;
                        data.Nombre     = ii.Nombre;

                        MySQLService.Instance.GetSession <GQ_Accesos>().Insert(data);
                    }
                    else
                    {
                        data.Nombre = ii.Nombre;
                        MySQLService.Instance.GetSession <GQ_Accesos>().Update(data);
                    }

                    attribute = (SecurityDescription)ii.extra;
                    if (attribute.Perfiles != null && attribute.Perfiles.Length > 0)
                    {
                        for (int i = 0; i < attribute.Perfiles.Length; i++)
                        {
                            var rol = roles[attribute.Perfiles[i]];
                            var ar  = rol.PerfilesAccesos.Where(x => x.AccesoId == data.Id).FirstOrDefault();

                            if (ar == null)
                            {
                                ar = new GQ_Perfiles_AccesosDto {
                                    PerfilId = rol.Id.Value, AccesoId = data.Id.Value, Permite = true
                                };
                                rol.PerfilesAccesos.Add(ar);
                                rol.Modificado = DateTime.Now;
                            }
                        }
                    }
                }

                foreach (var item in roles.Values)
                {
                    foreach (var itemA in item.PerfilesAccesos)
                    {
                        var itemInsert = itemA.GetEntity();
                        MySQLService.Instance.GetSession <GQ_Perfiles_Accesos>().Update(itemInsert);
                    }
                }

                ///
                ///
                /// CREACION DE USUARIO ADMINISTRADOR
                ///
                ///
                var UsuarioColl = MySQLService.Instance.GetSession <GQ_Usuarios>().findBy(x => true);

                var usuario = UsuarioColl.Where(x => x.NombreUsuario == "admin").FirstOrDefault();

                if (usuario == null)
                {
                    usuario = new GQ_Usuarios
                    {
                        Nombre             = "Admin",
                        Apellido           = "Admin",
                        NombreUsuario      = "admin",
                        Password           = Constantes.Encriptar("admin1234"),
                        RequiereContraseña = false,
                        Email      = "",
                        Estado     = Constantes.ESTADO_ACTIVO,
                        Creado     = DateTime.Now,
                        Modificado = DateTime.Now
                    };

                    MySQLService.Instance.GetSession <GQ_Usuarios>().Insert(usuario);

                    var up = new GQ_Usuarios_Perfiles
                    {
                        UsuarioId = usuario.Id.Value,
                        PerfilId  = roles[Security.ROL_ADMI].Id.Value,
                    };

                    MySQLService.Instance.GetSession <GQ_Usuarios_Perfiles>().Insert(up);
                }

                ///
                ///
                /// CREACION DE MENUES
                ///
                ///

                var MenuColl = MySQLService.Instance.GetSession <GQ_Menues>().findBy(x => true).ToList();

                var menu = MenuColl.Where(x => x.KeyName == MENU_CONFIG_ID).FirstOrDefault();

                if (menu == null)
                {
                    menu = new GQ_Menues();
                    menu.MenuPosition = MENU_CONFIG_ID;
                    menu.KeyName      = MENU_CONFIG_ID;
                    menu.Nombre       = "menu_configuracion";
                    menu.MenuIcono    = "fa fa-fw fa-cogs";
                    menu.Creado       = DateTime.Now;
                    menu.Modificado   = DateTime.Now;
                    menu.Estado       = Constantes.ESTADO_ACTIVO;
                    MySQLService.Instance.GetSession <GQ_Menues>().Insert(menu);
                }

                menu = MenuColl.Where(x => x.KeyName == MENU_ESTADISTICAS_ID).FirstOrDefault();

                if (menu == null)
                {
                    menu = new GQ_Menues();
                    menu.MenuPosition = MENU_ESTADISTICAS_ID;
                    menu.KeyName      = MENU_ESTADISTICAS_ID;
                    menu.Nombre       = "menu_tablero_control";
                    menu.MenuIcono    = "fa fa-fw fa-dashboard";
                    menu.Creado       = DateTime.Now;
                    menu.Modificado   = DateTime.Now;
                    menu.Estado       = Constantes.ESTADO_ACTIVO;
                    MySQLService.Instance.GetSession <GQ_Menues>().Insert(menu);
                }

                foreach (var m in menues.Keys)
                {
                    menu = MenuColl.Where(x => x.KeyName == m.Id).FirstOrDefault();
                    if (menu == null)
                    {
                        var obj = menues[m];

                        menu = new GQ_Menues();
                        menu.MenuPosition = m.Id;
                        menu.KeyName      = m.Id;
                        menu.Nombre       = m.Description;
                        menu.MenuIcono    = "";
                        menu.MenuPadre    = m.IdParent;
                        menu.Creado       = DateTime.Now;
                        menu.Modificado   = DateTime.Now;
                        menu.Estado       = Constantes.ESTADO_ACTIVO;

                        if (obj is Type)
                        {
                            menu.MenuUrl = ((Type)obj).Name.Replace("Controller", "");
                        }
                        else if (obj is MethodInfo)
                        {
                            menu.MenuUrl = ((MethodInfo)obj).ReflectedType.Name.Replace("Controller", "") + @"/" + ((MethodInfo)obj).Name;
                        }

                        MySQLService.Instance.GetSession <GQ_Menues>().Insert(menu);
                    }
                }
            }
        }
コード例 #5
0
ファイル: Security.cs プロジェクト: difros/REPO-ts-day
        public static bool hasPermission(object value, string method, bool returnException = true, params object[] parameters)
        {
            bool result   = true;
            bool isLogued = Security.usuarioLogueado != null;

            Type type = (Type)(value is Type ? value : value.GetType().GetTypeInfo());

            SecurityDescription attribute = (SecurityDescription)type.GetCustomAttribute(typeof(SecurityDescription), true);

            if (attribute != null)
            {
                if (attribute.Estado == SecurityDescription.SeguridadEstado.Activo)
                {
                    result = result && _hasPermission(type, null, returnException);
                }
                else if (attribute.Estado == SecurityDescription.SeguridadEstado.SoloLogueo && isLogued == false)
                {
                    result = false;
                }

                if (result && method != null)
                {
                    var methodFind = ClassUtils.GetMethod(type, method);
                    if (methodFind.Length > 0)
                    {
                        attribute = (SecurityDescription)methodFind[0].GetCustomAttribute(typeof(SecurityDescription), true);
                        if (attribute != null)
                        {
                            if (attribute.Estado == SecurityDescription.SeguridadEstado.Activo)
                            {
                                result = result && _hasPermission(type, method, returnException);
                            }
                            else if (attribute.Estado == SecurityDescription.SeguridadEstado.ActivoFunction)
                            {
                                result = result && _hasPermission(type, method, returnException);
                                result = result && (bool)attribute.Method.Invoke(null, parameters);
                            }
                            else if (attribute.Estado == SecurityDescription.SeguridadEstado.SoloLogueo && isLogued == false)
                            {
                                result = false;
                            }
                        }
                        else
                        {
                            result = false;
                        }
                    }
                    else
                    {
                        result = false;
                    }
                }
            }
            else
            {
                result = false;
            }

            if (result == false && returnException)
            {
                throw new SecurityException(value, method);
            }

            return(result);
        }
コード例 #6
0
ファイル: Security.cs プロジェクト: difros/REPO-ts-day
        public static void CreateAccessSecurity()
        {
            using (var transaction = Services.session.BeginTransaction())
            {
                Assembly assembly = typeof(Security).GetTypeInfo().Assembly;

                SecurityDescription attribute     = null;
                MenuDescription     menuAttribute = null;

                Dictionary <string, Gq_perfiles>     roles  = new Dictionary <string, Gq_perfiles>();
                Dictionary <MenuDescription, object> menues = new Dictionary <MenuDescription, object>();
                ArrayList items = new ArrayList();

                Gq_accesosDto itemClass  = null;
                Gq_accesosDto itemMethod = null;

                #region CREACION DE ROLES

                if (ROLES.Length > 0)
                {
                    for (int i = 0; i < ROLES.Length; i++)
                    {
                        if (roles.ContainsKey(ROLES[i]) == false)
                        {
                            roles.Add(ROLES[i], null);
                        }
                    }
                }

                for (int i = 0; i < roles.Keys.Count; i++)
                {
                    string key = roles.Keys.ElementAt(i);
                    var    rol = Services.Get <ServGq_perfiles>().findBy(x => x.KeyName == key).FirstOrDefault();

                    if (rol == null)
                    {
                        rol               = new Gq_perfiles();
                        rol.Nombre        = key;
                        rol.KeyName       = key;
                        rol.Estado        = Constantes.ESTADO_ACTIVO;
                        rol.Creado        = DateTime.Now;
                        rol.Modificado    = DateTime.Now;
                        rol.CreadoPor     = -1;
                        rol.ModificadoPor = -1;

                        Services.Get <ServGq_perfiles>().Agregar(rol);

                        rol = Services.Get <ServGq_perfiles>().findBy(x => x.KeyName == key).FirstOrDefault();
                    }

                    roles[key] = rol;
                }
                #endregion

                #region CREACION DE ACCESOS

                var elementsType = (from iface in assembly.GetTypes()
                                    where iface.Namespace != null && iface.Namespace.Contains("Controllers")
                                    select iface);

                foreach (Type item in elementsType)
                {
                    menuAttribute = (MenuDescription)item.GetTypeInfo().GetCustomAttribute(typeof(MenuDescription), false);
                    if (menuAttribute != null)
                    {
                        menues.Add(menuAttribute, item);
                    }
                    attribute = (SecurityDescription)item.GetTypeInfo().GetCustomAttribute(typeof(SecurityDescription), false);

                    if (attribute != null)
                    {
                        itemClass       = new Gq_accesosDto();
                        itemClass.Clase = getNameObject(item);

                        if (attribute.Estado == SecurityDescription.SeguridadEstado.Activo)
                        {
                            itemClass.Metodo      = null;
                            itemClass.Nombre      = attribute.Name;
                            itemClass.extra       = attribute;
                            itemClass.Descripcion = "Class :" + attribute.Name;

                            //obtenerProfiles(attribute, roles);

                            items.Add(itemClass);
                        }

                        foreach (MethodInfo method in item.GetMethods())
                        {
                            menuAttribute = (MenuDescription)method.GetCustomAttribute(typeof(MenuDescription), true);
                            if (menuAttribute != null)
                            {
                                menues.Add(menuAttribute, method);
                            }

                            attribute = (SecurityDescription)method.GetCustomAttribute(typeof(SecurityDescription), true);
                            if (attribute != null && attribute.Estado == SecurityDescription.SeguridadEstado.Activo)
                            {
                                itemMethod             = new Gq_accesosDto();
                                itemMethod.Clase       = itemClass.Clase;
                                itemMethod.Metodo      = method.Name;
                                itemMethod.Nombre      = attribute.Name;
                                itemMethod.Descripcion = "Method :" + attribute.Name;
                                itemMethod.extra       = attribute;

                                //obtenerProfiles(attribute, roles);

                                items.Add(itemMethod);
                            }
                        }
                    }
                }

                foreach (Gq_accesosDto ii in items)
                {
                    var data = Services.Get <ServGq_accesos>().findBy(x => x.Clase == ii.Clase && x.Metodo == ii.Metodo).FirstOrDefault();
                    if (data == null)
                    {
                        data             = new Gq_accesos();
                        data.Clase       = ii.Clase;
                        data.Metodo      = ii.Metodo;
                        data.Descripcion = ii.Descripcion;
                        data.Nombre      = ii.Nombre;

                        Services.Get <ServGq_accesos>().Agregar(data);


                        data = Services.Get <ServGq_accesos>().findBy(x => x.Clase == ii.Clase && x.Metodo == ii.Metodo).FirstOrDefault();
                    }
                    else
                    {
                        data.Nombre = ii.Nombre;

                        Services.Get <ServGq_accesos>().Actualizar(data);
                    }

                    attribute = (SecurityDescription)ii.extra;
                    if (attribute.Perfiles != null && attribute.Perfiles.Length > 0)
                    {
                        for (int i = 0; i < attribute.Perfiles.Length; i++)
                        {
                            var dataPerfiles = Services.Get <ServGq_perfiles_accesos>().findBy(x => x.AccesoId == data.AccesoId && x.PerfilId == roles[attribute.Perfiles[i]].PerfilId).FirstOrDefault();
                            if (dataPerfiles == null)
                            {
                                dataPerfiles                = new Gq_perfiles_accesos();
                                dataPerfiles.AccesoId       = data.AccesoId.Value;
                                dataPerfiles.PerfilId       = roles[attribute.Perfiles[i]].PerfilId.Value;
                                dataPerfiles.GrantPermition = "1";
                                dataPerfiles.Estado         = "A";
                                dataPerfiles.Creado         = DateTime.Now;
                                dataPerfiles.Modificado     = DateTime.Now;
                                dataPerfiles.CreadoPor      = dataPerfiles.ModificadoPor = -1;

                                Services.Get <ServGq_perfiles_accesos>().Agregar(dataPerfiles);
                            }
                        }
                    }
                }


                #endregion

                #region CREACION DE USUARIO ADMINISTRADOR

                var usuario = Services.Get <ServGq_usuarios>().findBy(x => x.Usuario == "admin").FirstOrDefault();

                if (usuario == null)
                {
                    usuario = new Gq_usuarios
                    {
                        Nombre        = "Admin",
                        Apellido      = "Admin",
                        Usuario       = "admin",
                        Clave         = Encriptacion.Encriptar("admin1234", Constantes.CLAVE_ENCRIPTACION),
                        Email         = "",
                        Estado        = Constantes.ESTADO_ACTIVO,
                        PerfilId      = roles[ROL_ADMI].PerfilId.Value,
                        Creado        = DateTime.Now,
                        Modificado    = DateTime.Now,
                        CreadoPor     = -1,
                        ModificadoPor = -1,
                        RequiereClave = "N",
                    };

                    Services.Get <ServGq_usuarios>().Agregar(usuario);
                }


                #endregion

                #region CREACION DE MENUES

                var menu = Services.Get <ServGq_menu>().findBy(x => x.KeyName == MENU_CONFIG_ID).FirstOrDefault();

                if (menu == null)
                {
                    menu = new Gq_menu();
                    menu.MenuPosition  = MENU_CONFIG_ID;
                    menu.KeyName       = MENU_CONFIG_ID;
                    menu.Nombre        = "Menu";
                    menu.MenuIcono     = "fa fa-fw fa-cogs";
                    menu.Creado        = DateTime.Now;
                    menu.Modificado    = DateTime.Now;
                    menu.CreadoPor     = -1;
                    menu.ModificadoPor = -1;
                    menu.Estado        = Constantes.ESTADO_ACTIVO;

                    Services.Get <ServGq_menu>().Agregar(menu);
                }

                foreach (var m in menues.Keys)
                {
                    menu = Services.Get <ServGq_menu>().findBy(x => x.KeyName == m.Id).FirstOrDefault();
                    if (menu == null)
                    {
                        var obj = menues[m];

                        menu = new Gq_menu();
                        menu.MenuPosition  = m.Id;
                        menu.Nombre        = m.Description;
                        menu.KeyName       = m.Id;
                        menu.MenuIcono     = "";
                        menu.MenuPadre     = m.IdParent;
                        menu.Creado        = DateTime.Now;
                        menu.Modificado    = DateTime.Now;
                        menu.CreadoPor     = -1;
                        menu.ModificadoPor = -1;
                        menu.Estado        = Constantes.ESTADO_ACTIVO;

                        if (obj is Type)
                        {
                            menu.MenuUrl = ((Type)obj).Name.Replace("Controller", "");
                        }
                        else if (obj is MethodInfo)
                        {
                            menu.MenuUrl = ((MethodInfo)obj).DeclaringType.Name.Replace("Controller", "") + @"/" + ((MethodInfo)obj).Name;
                        }

                        Services.Get <ServGq_menu>().Agregar(menu);
                    }
                }
                #endregion

                transaction.Commit();
            }
        }