Exemple #1
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));
                }
            }
        }
Exemple #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));
         }
     }
 }
Exemple #3
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));
                }
            }
        }