public ActionResult EditMenuItem(int id) //Id del item
        {
            var context            = new AppSecurityContext();
            var menuRepository     = new MenuRepository(context);
            var menuItemRepository = new MenuItemRepository(context);

            var menuItem = menuItemRepository.Find(id);
            var model    = mapper.Map <EditMenuItemViewModel>(menuItem);

            model.MenuKey              = menuItem.AppMenuKey;
            model.MenuName             = menuItem.AppMenu.Name;
            model.AvailablePermissions = PopulatePermissions(model.PermissionId);
            model.AvailableMenuItems   = PopulateMenuItems(model.ParentId);

            model.AvailablePermissions = PopulatePermissions(model.PermissionId);
            model.AvailableMenuItems   = PopulateMenuItems(model.ParentId);

            var itemSelf = model.AvailableMenuItems.Where(x => x.Value == id.ToString());

            if (itemSelf != null && itemSelf.Count() > 0)
            {
                var itemForRemove = itemSelf.SingleOrDefault();
                model.AvailablePermissions.ToList().Remove(itemForRemove);
            }

            return(View(model));
        }
Exemplo n.º 2
0
 public ActionResult CreatePermission(NewAppPermissionViewModel model)
 {
     using (var context = new AppSecurityContext())
     {
         var actionRepository     = new ActionRepository(context);
         var resourceRepository   = new ResourceRepository(context);
         var permissionRepository = new PermissionRepository(context);
         try
         {
             var resource = resourceRepository.Find(model.SelectedResource);
             var actions  = actionRepository.Query(x => model.SelectedActions.Contains(x.Key));
             foreach (var action in actions)
             {
                 var permission = new AppPermission();
                 permission.Action   = action;
                 permission.Resource = resource;
                 permissionRepository.Add(permission);
             }
             context.SaveChanges();
             return(RedirectToAction("Permissions"));
         }
         catch (Exception ex)
         {
             ViewBag.Error = ex.Message;
             var actions = actionRepository.GetAll().OrderBy(x => x.Name);
             model.AvailableActions = mapper.Map <ICollection <AppActionViewModel> >(actions);
             model.ResourcesList    = PopulateResourcesForNewPermission(model.SelectedResource);
             return(View(model));
         }
     }
 }
Exemplo n.º 3
0
        public ActionResult UserPermission(string id)
        {
            try
            {
                using (var context = new AppSecurityContext())
                {
                    var userPermissionRep = new UserPermissionRepository(context);
                    var permissionRep     = new PermissionRepository(context);
                    var userRep           = new UserRepository(context);

                    var permissions = userPermissionRep.GetAll()
                                      .Where(x => x.UserId == id);

                    var user = userRep.Find(id);

                    var model = new ListAppUserPermissionViewModel();
                    model.UserId          = user.Id;
                    model.UserName        = user.UserName;
                    model.UserPermissions = mapper.Map <IEnumerable <AppUserPermissionViewModel> >(permissions);
                    return(View(model));
                }
            }
            catch
            {
                return(View("Users"));
            }
        }
Exemplo n.º 4
0
        public ActionResult CreateUserPermission(string id)
        {
            var model = new NewAppUserPermissionViewModel();

            using (var context = new AppSecurityContext())
            {
                var resourcesRepository    = new ResourceRepository(context);
                var userPermissionRository = new UserPermissionRepository(context);
                var userRepository         = new UserRepository(context);
                var actionRepository       = new ActionRepository(context);

                var user = userRepository.Find(id);
                if (user == null)
                {
                    RedirectToAction("Users");
                }

                var actions = actionRepository.GetAll().OrderBy(x => x.Name);
                model.AvailableActions = mapper.Map <ICollection <AppActionViewModel> >(actions);
                model.User             = mapper.Map <AppUserViewModel>(user);
                model.ResourcesList    = PopulateResourceFromNewUserPermission(id);

                return(View(model));
            }
        }
Exemplo n.º 5
0
        public ActionResult CreateUserPermission(NewAppUserPermissionViewModel model)
        {
            using (var context = new AppSecurityContext())
            {
                var actionRepository         = new ActionRepository(context);
                var permissionRepository     = new PermissionRepository(context);
                var userPermissionRepository = new UserPermissionRepository(context);
                var userRepository           = new UserRepository(context);

                try
                {
                    var actions    = actionRepository.Query(x => model.SelectedActions.Contains(x.Key)).Select(x => x.Key);
                    var permission = permissionRepository
                                     .GetAll()
                                     .Where(x => x.ResourceKey == model.SelectedResource)
                                     .Where(x => actions.Contains(x.ActionKey));

                    userPermissionRepository.AddPermissionsByUserId(model.User.Id, permission.Select(x => x.Id).ToArray());
                    context.SaveChanges();
                    return(RedirectToAction("UserPermission", new { id = model.User.Id }));
                }
                catch (Exception ex)
                {
                    ViewBag.Error = ex.Message;
                    var actions = actionRepository.GetAll().OrderBy(x => x.Name);
                    var user    = userRepository.Find(model.User.Id);
                    model.User             = mapper.Map <AppUserViewModel>(user);
                    model.AvailableActions = mapper.Map <ICollection <AppActionViewModel> >(actions);
                    model.ResourcesList    = PopulateResourceFromNewUserPermission(model.SelectedResource);
                    return(View(model));
                }
            }
        }
Exemplo n.º 6
0
        public ActionResult CreateRole(NewAppRoleViewModel model)
        {
            var context                  = new AppSecurityContext();
            var rolRepository            = new RoleRepository(context);
            var permissionRepository     = new PermissionRepository(context);
            var rolePermissionRepository = new RolePermissionRepository(context);

            if (ModelState.IsValid)
            {
                var role = mapper.Map <AppRole>(model);
                role.Id = Guid.NewGuid().ToString();
                rolRepository.Add(role);
                if (model.SelectedPermissions == null)
                {
                    model.SelectedPermissions = new int[0];
                }

                foreach (var permissionId in model.SelectedPermissions)
                {
                    rolePermissionRepository.Add(new AppRolePermission {
                        PermissionId = permissionId, RoleId = role.Id
                    });
                }
                context.SaveChanges();
                return(RedirectToAction("Roles", "SecuritySettings"));
            }

            var permissions = permissionRepository.GetAll();

            model.AvailablePermissions = mapper.Map <ICollection <AppPermissionViewModel> >(permissions);
            return(View(model));
        }
Exemplo n.º 7
0
        public ActionResult EditMenuItem(int id, EditMenuItemViewModel model)
        {
            var context            = new AppSecurityContext();
            var menuRepository     = new MenuRepository(context);
            var menuItemRepository = new MenuItemRepository(context);

            try
            {
                if (ModelState.IsValid)
                {
                    var menuItem = mapper.Map <AppMenuItem>(model);
                    menuItemRepository.Update(menuItem);
                    context.SaveChanges();
                    return(RedirectToAction("menuItems", new { id = model.MenuKey }));
                }
            }
            catch (Exception ex)
            {
                ViewBag.Error = ex.Message;
            }



            model.AvailablePermissions = PopulatePermissions(model.PermissionId);
            model.AvailableMenuItems   = PopulateMenuItems(model.ParentId);

            var itemSelf = model.AvailableMenuItems.Where(x => x.Value == id.ToString());

            if (itemSelf != null && itemSelf.Count() > 0)
            {
                var itemForRemove = itemSelf.SingleOrDefault();
                model.AvailablePermissions.ToList().Remove(itemForRemove);
            }
            return(View(model));
        }
Exemplo n.º 8
0
        public ActionResult DetailsUser(string id)
        {
            var context           = new AppSecurityContext();
            var userRepository    = new UserRepository(context);
            var roleRepository    = new RoleRepository(context);
            var userPermissionRep = new UserPermissionRepository(context);

            var user  = userRepository.Find(id);
            var roles = roleRepository.GetRolesByUserId(user.Id);
            var model = new DetailsAppUserViewModel();

            model.Email         = user.Email;
            model.Id            = user.Id;
            model.AssignedRoles = new List <AppRoleViewModel>();
            foreach (var item in roles)
            {
                model.AssignedRoles.Add(new AppRoleViewModel {
                    Id = item.Id, Name = item.Name
                });
            }
            var permissions = userPermissionRep.GetAll()
                              .Where(x => x.UserId == id);

            model.UserPermissions = mapper.Map <IEnumerable <AppUserPermissionViewModel> >(permissions);
            return(View(model));
        }
Exemplo n.º 9
0
 public ActionResult CreateOrUpdateResource(string id, AppResourceViewModel model)
 {
     using (var context = new AppSecurityContext())
     {
         try
         {
             var resource   = mapper.Map <AppResource>(model);
             var repository = new ResourceRepository(context);
             if (id != null)
             {
                 repository.Update(resource);
             }
             else
             {
                 repository.Insert(resource);
             }
             context.SaveChanges();
             return(RedirectToAction("Resources"));
         }
         catch (Exception ex)
         {
             ViewBag.Error = ex.Message;
             return(View(model));
         }
     }
 }
Exemplo n.º 10
0
        public ActionResult EditUser(string id)
        {
            var context           = new AppSecurityContext();
            var rolRepository     = new RoleRepository(context);
            var userRolRepository = new UserRoleRepository(context);

            var user  = context.Users.Find(id);
            var model = new EditAppUserViewModel();

            model.Email = user.Email;
            model.Id    = user.Id;

            var roles = rolRepository.GetAll();

            var assignedRoles = userRolRepository.GetAssignedUserRoles(id);

            if (assignedRoles.Count() > 0)
            {
                model.SelectedRoles = assignedRoles.Select(x => x.RoleId).ToArray();
            }
            else
            {
                model.SelectedRoles = new string[0];
            }
            model.AvailableRoles = mapper.Map <ICollection <AppRoleViewModel> >(roles);
            return(View(model));
        }
Exemplo n.º 11
0
        public ActionResult EditPermission(string id)
        { //Clave del resource
            var model = new EditAppPermissionViewModel();

            using (var context = new AppSecurityContext())
            {
                var actionRepository     = new ActionRepository(context);
                var resourceRepository   = new ResourceRepository(context);
                var permissionRepository = new PermissionRepository(context);
                try
                {
                    var actions     = actionRepository.GetAll();
                    var resource    = resourceRepository.Find(id);
                    var permissions = permissionRepository.GetAll().Where(x => x.ResourceKey == resource.Key);
                    var actionKeys  = permissions.Select(x => x.ActionKey).ToArray();
                    model.AvailableActions = mapper.Map <ICollection <AppActionViewModel> >(actions);
                    model.SelectedActions  = actionKeys;
                    model.ResourceKey      = resource.Key;
                    model.ResourceName     = resource.Name;

                    return(View(model));
                }
                catch (Exception ex)
                {
                    ViewBag.Error = ex.Message;
                    return(View(model));
                }
            }
        }
Exemplo n.º 12
0
        public ActionResult Users()
        {
            AppSecurityContext context = new AppSecurityContext();
            var users = UserManager.Users;
            var model = mapper.Map <IEnumerable <AppUserViewModel> >(users);

            return(View(model));
        }
Exemplo n.º 13
0
        public ActionResult EditMenu(string id)
        {
            var context        = new AppSecurityContext();
            var menuRepository = new MenuRepository(context);
            var menu           = menuRepository.Find(id);
            var model          = mapper.Map <EditMenuViewModel>(menu);

            return(View(model));
        }
Exemplo n.º 14
0
        public ActionResult Menus()
        {
            var context        = new AppSecurityContext();
            var menuRepository = new MenuRepository(context);
            var menus          = menuRepository.GetAll();
            var model          = mapper.Map <ICollection <MenuViewModel> >(menus);

            return(View(model));
        }
Exemplo n.º 15
0
        public ActionResult Roles()
        {
            var context       = new AppSecurityContext();
            var rolRepository = new RoleRepository(context);
            var roles         = rolRepository.GetAll();
            var models        = mapper.Map <IEnumerable <AppRoleViewModel> >(roles);

            return(View(models));
        }
Exemplo n.º 16
0
        public ActionResult CreateRole()
        {
            var context = new AppSecurityContext();
            var model   = new NewAppRoleViewModel();
            var permissionRepository = new PermissionRepository(context);
            var permissions          = permissionRepository.GetAll();

            model.AvailablePermissions = mapper.Map <ICollection <AppPermissionViewModel> >(permissions);
            return(View(model));
        }
Exemplo n.º 17
0
 public ActionResult Permissions()
 {
     using (var context = new AppSecurityContext())
     {
         var repository  = new PermissionRepository(context);
         var permissions = repository.GetAll();
         var model       = mapper.Map <IEnumerable <AppPermissionViewModel> >(permissions);
         return(View(model));
     }
 }
Exemplo n.º 18
0
 public ActionResult Resources()
 {
     using (var context = new AppSecurityContext())
     {
         var repository = new ResourceRepository(context);
         var resources  = repository.GetAll();
         var model      = mapper.Map <IEnumerable <AppResourceViewModel> >(resources);
         return(View(model));
     }
 }
Exemplo n.º 19
0
        public SelectList PopulateMenuItems(object selectedItem = null)
        {
            var context     = new AppSecurityContext();
            var repository  = new MenuItemRepository(context);
            var permissions = repository.GetAll().OrderBy(x => x.Name).ToList();

            permissions.Insert(0, new AppMenuItem {
                Id = null, Name = "Sin padre"
            });
            return(new SelectList(permissions, "Id", "Name", selectedItem));
        }
Exemplo n.º 20
0
 public SelectList PopulateResources(object selectedItem = null)
 {
     using (var context = new AppSecurityContext())
     {
         var repository = new ResourceRepository(context);
         var resources  = repository.GetAll().OrderBy(x => x.Name).ToList();
         resources.Insert(0, new AppResource {
             Key = null, Name = "Seleccione"
         });
         return(new SelectList(resources, "Key", "Name", selectedItem));
     }
 }
Exemplo n.º 21
0
        public ActionResult CreateMenuItem(string id)
        {
            var context        = new AppSecurityContext();
            var menuRepository = new MenuRepository(context);
            var menu           = menuRepository.Find(id);
            var model          = new NewMenuItemViewModel();

            model.MenuName             = menu.Name;
            model.MenuKey              = menu.Key;
            model.AvailablePermissions = PopulatePermissions(model.PermissionId);
            model.AvailableMenuItems   = PopulateMenuItems(model.ParentId);
            return(View(model));
        }
Exemplo n.º 22
0
        public ActionResult CreateMenu(NewMenuViewModel model)
        {
            var context        = new AppSecurityContext();
            var menuRepository = new MenuRepository(context);

            if (ModelState.IsValid)
            {
                var menu = mapper.Map <AppMenu>(model);
                menuRepository.Add(menu);
                context.SaveChanges();
                return(RedirectToAction("Menus"));
            }
            return(View());
        }
Exemplo n.º 23
0
        public ActionResult CreatePermission()
        {
            var model = new NewAppPermissionViewModel();

            using (var context = new AppSecurityContext())
            {
                var actionRepository = new ActionRepository(context);
                var actions          = actionRepository.GetAll().OrderBy(x => x.Name);

                model.AvailableActions = mapper.Map <ICollection <AppActionViewModel> >(actions);
                model.ResourcesList    = PopulateResourcesForNewPermission(model.SelectedResource);
                return(View(model));
            }
        }
Exemplo n.º 24
0
        public ActionResult MenuItems(string id)
        {
            var context            = new AppSecurityContext();
            var menuRepository     = new MenuRepository(context);
            var menuItemRepository = new MenuItemRepository(context);
            var menu  = menuRepository.Find(id);
            var items = menuItemRepository.GetItemsByMenuKey(id);
            var model = new MenuItemListViewModel();

            model.MenuItems = mapper.Map <ICollection <MenuItemViewModel> >(items);
            model.MenuKey   = menu.Key;
            model.MenuName  = menu.Name;
            return(View(model));
        }
Exemplo n.º 25
0
        public ActionResult EditPermission(string id, EditAppPermissionViewModel model)
        { //Clave del resource
            using (var context = new AppSecurityContext())
            {
                var actionRepository     = new ActionRepository(context);
                var resourceRepository   = new ResourceRepository(context);
                var permissionRepository = new PermissionRepository(context);
                try
                {
                    var actions     = actionRepository.GetAll();
                    var resource    = resourceRepository.Find(id);
                    var permissions = permissionRepository.GetAll().Where(x => x.ResourceKey == resource.Key);
                    var actionKeys  = permissions.Select(x => x.ActionKey).ToArray();
                    model.AvailableActions = mapper.Map <ICollection <AppActionViewModel> >(actions);

                    //Se eliminan los permisos anteriores
                    foreach (var perm in permissions)
                    {
                        permissionRepository.Delete(perm);
                    }

                    if (model.SelectedActions != null)
                    {
                        //Se agregan los nuevos
                        var actionsForInsert = actionRepository.Query(x => model.SelectedActions.Contains(x.Key));
                        foreach (var action in actionsForInsert)
                        {
                            var permission = new AppPermission();
                            permission.Action   = action;
                            permission.Resource = resource;
                            permissionRepository.Add(permission);
                        }
                        model.SelectedActions = actionKeys;
                        model.ResourceKey     = resource.Key;
                        model.ResourceName    = resource.Name;
                    }
                    context.SaveChanges();
                    return(RedirectToAction("Permissions"));
                }
                catch (Exception ex)
                {
                    ViewBag.Error = ex.Message;
                    if (ex.InnerException != null)
                    {
                        ViewBag.Error += ex.InnerException.Message;
                    }
                    return(View(model));
                }
            }
        }
Exemplo n.º 26
0
 public SelectList PopulateResourcesForNewPermission(object selectedItem = null)
 {
     using (var context = new AppSecurityContext())
     {
         var repository           = new ResourceRepository(context);
         var permissionRepository = new PermissionRepository(context);
         var resourcesId          = permissionRepository.GetAll().Select(x => x.ResourceKey);
         var resources            = repository.Query(x => !resourcesId.Contains(x.Key)).ToList();
         resources.Insert(0, new AppResource {
             Key = null, Name = "Seleccione"
         });
         return(new SelectList(resources, "Key", "Name", selectedItem));
     }
 }
Exemplo n.º 27
0
        /// <summary>
        /// Verifica si un usuario tiene o no permiso para realizar una acción
        /// </summary>
        /// <param name="context">Contexto de seguridad</param>
        /// <param name="actionKey">Clave de la acción que se desea realizar</param>
        /// <param name="resourceKey">Clave del recurso que se desea acceder</param>
        /// <returns>true/false</returns>
        public static bool HasPermission(this IOwinContext context, string actionKey, string resourceKey)
        {
            var user = (ClaimsIdentity)HttpContext.Current.User.Identity;

            if (!user.IsAuthenticated)
            {
                return(false);
            }
            var userId = user.Claims.SingleOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
            AppSecurityContext contextSecurity = new AppSecurityContext();
            long id         = long.Parse(userId);
            var  repository = new PermissionRepository(contextSecurity);
            var  valid      = repository.HasPermission(id, actionKey, resourceKey);

            return(valid);
        }
Exemplo n.º 28
0
        public ActionResult CreateOrUpdateResource(string id)
        {
            var model = new AppResourceViewModel();

            if (id != null)
            {
                using (var context = new AppSecurityContext())
                {
                    var repository = new ResourceRepository(context);
                    var resource   = repository.Find(id);
                    model = mapper.Map <AppResourceViewModel>(resource);
                    return(View(model));
                }
            }
            return(View(model));
        }
Exemplo n.º 29
0
        public SelectList PopulateResourceFromNewUserPermission(string userId, object selectedItem = null)
        {
            using (var context = new AppSecurityContext())
            {
                var resourcesRepository    = new ResourceRepository(context);
                var userPermissionRository = new UserPermissionRepository(context);

                var permissionSelect = userPermissionRository.GetPermissionByUserId(userId).Select(p => p.ResourceKey).Distinct();
                var resourcesQuery   = resourcesRepository.GetAll();
                var resources        = resourcesQuery.Where(x => !permissionSelect.Contains(x.Key)).ToList();
                resources.Insert(0, new AppResource {
                    Key = null, Name = "Seleccione"
                });
                return(new SelectList(resources, "Key", "Name", selectedItem));
            }
        }
Exemplo n.º 30
0
        /// <summary>
        /// Se ejecuta cuando se va a validar si se tiene o no acceso a lo solicitado
        /// </summary>
        /// <param name="httpContext">Contexo Http</param>
        /// <returns>true/false</returns>
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            var user = (ClaimsIdentity)HttpContext.Current.User.Identity;

            if (!user.IsAuthenticated)
            {
                return(false);
            }
            var userId = user.Claims.SingleOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
            AppSecurityContext context = new AppSecurityContext();
            long id         = long.Parse(userId);
            var  repository = new PermissionRepository(context);
            var  valid      = repository.HasPermission(id, ActionKey, ResourceKey);

            return(valid);
        }