Example #1
0
        //GET --------------------------------------------------------------------------------------
        public IActionResult Index()
        {
            RolesRepository   rolesRepository  = new RolesRepository();
            IEnumerable <Rol> rolesIEnumerable = rolesRepository.GetAll();

            return(View(rolesIEnumerable));
        }
        public IHttpActionResult GetAll()
        {
            var roles       = _rolesRepository.GetAll();
            var resultRoles = roles.Select(role => new { role.Id, Role = role.Name });

            return(Ok(resultRoles));
        }
Example #3
0
        private void UpdateUserRoles(string[] assignedRoles, User user, RolesRepository rolesRepository)
        {
            if (assignedRoles == null)
            {
                return;
            }

            var assignedRolesHS = new HashSet <string>(assignedRoles);
            var userRoles       = new HashSet <int>(user.Roles.Select(u => u.ID));

            foreach (var role in rolesRepository.GetAll())
            {
                if (assignedRolesHS.Contains(role.ID.ToString()))
                {
                    if (!userRoles.Contains(role.ID))
                    {
                        user.Roles.Add(role);
                    }
                }
                else if (userRoles.Contains(role.ID))
                {
                    user.Roles.Remove(role);
                }
            }
        }
Example #4
0
        public ActionResult EditRole(int id)
        {
            LibraryManagementSystemContext context = new LibraryManagementSystemContext();
            RolesRepository rolesRepository        = new RolesRepository(context);
            AuthenticatingActionsRepository authenticatingActionsRepository = new AuthenticatingActionsRepository(context);

            RolesEditRoleVM model = new RolesEditRoleVM();

            if (id > 0)
            {
                Role role = rolesRepository.GetAll(filter: r => r.ID == id, includeProperties: "AuthenticatingActions").FirstOrDefault();
                PopulateAssignedAuthenticatingActions(role, authenticatingActionsRepository);

                model.ID   = role.ID;
                model.Name = role.Name;
            }
            else
            {
                Role role = new Role();
                role.AuthenticatingActions = new List <AuthenticatingAction>();
                PopulateAssignedAuthenticatingActions(role, authenticatingActionsRepository);
            }

            return(View(model));
        }
Example #5
0
        static void Main(string[] args)
        {
            string coString = "Data Source=localhost;Initial Catalog=DATABASE_TASK_2;Integrated Security=True";

            Console.WriteLine("----Roles----\n");
            RolesRepository rolesRepo = new RolesRepository(coString);

            var allRoles = rolesRepo.GetAll();

            foreach (var it in allRoles)
            {
                Console.WriteLine(it.ToString());
            }
            Console.WriteLine("-------------------------------------------------------------\n");
            Console.WriteLine("----Users----\n");
            UsersRepository usersRepo = new UsersRepository(coString);

            var allUsers = usersRepo.GetAll();

            foreach (var it in allUsers)
            {
                Console.WriteLine(it.ToString());
            }

            Console.WriteLine((rolesRepo.Get(0) != null) ? rolesRepo.Get(1).ToString() : "no role with such id");

            Console.ReadLine();
        }
Example #6
0
                                                   [HttpGet] public async Task <IHttpActionResult> GetAll()
                                                   {
                                                       try {
                                                           var campo = await _repository.GetAll();

                                                           return(Ok(campo));
                                                       }
                                                       catch (Exception e) { log.Error(new MDCSet(this.ControllerContext.RouteData), e);
                                                                             return(InternalServerError(e)); }
                                                   }
Example #7
0
 public IEnumerable <SystemRole> GetSystemRoles()
 {
     return(rolesRepository
            .GetAll()
            .Select(r => new SystemRole()
     {
         RoleName = r.RoleName,
         RoleId = r.RoleId,
         IsActive = r.IsActive,
         CreatedDate = r.CreatedDate
     }));
 }
        public ActionResult DeleteRole(int id)
        {
            LibraryManagementSystemContext context = new LibraryManagementSystemContext();
            RolesRepository rolesRepository = new RolesRepository(context);

            RolesDeleteRoleVM model = new RolesDeleteRoleVM();
            Role role = rolesRepository.GetAll(filter: r => r.ID == id, includeProperties: "AuthenticatingActions").FirstOrDefault();
            model.ID = role.ID;
            model.Name = role.Name;

            return View(model);
        }
Example #9
0
        public ActionResult DeleteRole(int id)
        {
            LibraryManagementSystemContext context = new LibraryManagementSystemContext();
            RolesRepository rolesRepository        = new RolesRepository(context);

            RolesDeleteRoleVM model = new RolesDeleteRoleVM();
            Role role = rolesRepository.GetAll(filter: r => r.ID == id, includeProperties: "AuthenticatingActions").FirstOrDefault();

            model.ID   = role.ID;
            model.Name = role.Name;

            return(View(model));
        }
Example #10
0
        public ActionResult Index(int?id)
        {
            RolesRepository _rolesRepository = new RolesRepository(_context);

            if (id == null)
            {
                var lst = _rolesRepository.GetAll(currentAccount, LanguageId).ToList();
                if (lst.Count > 0)
                {
                    ViewBag.RolesId = new SelectList(lst, "RolesId", "RolesName");
                    PageSelectData(lst.First());
                    ViewBag.id = lst.First().RolesId;
                }
                return(View());
            }
            else
            {
                int RolesId = Convert.ToInt32(id);
                try
                {
                    RolesModel rolesmodel = _rolesRepository.Find(RolesId);
                    if (rolesmodel == null)
                    {
                        return(HttpNotFound());
                    }

                    ViewBag.RolesId = new SelectList(_rolesRepository.GetAll(currentAccount, LanguageId), "RolesId", "RolesName", RolesId);
                    ViewBag.id      = RolesId;
                    PageSelectData(rolesmodel);

                    return(View());
                }
                catch
                {
                    return(HttpNotFound());
                }
            }
        }
        public ActionResult DeleteRole(RolesDeleteRoleVM model)
        {
            LibraryManagementSystemContext context = new LibraryManagementSystemContext();
            RolesRepository rolesRepository = new RolesRepository(context);

            Role role = rolesRepository.GetAll(filter: r => r.ID == model.ID, includeProperties: "AuthenticatingActions").FirstOrDefault();
            if (role == null)
            {
                return HttpNotFound();
            }

            role.AuthenticatingActions = null;
            rolesRepository.Delete(role);

            return RedirectToAction("Index", "Roles");
        }
Example #12
0
        public ActionResult DeleteRole(RolesDeleteRoleVM model)
        {
            LibraryManagementSystemContext context = new LibraryManagementSystemContext();
            RolesRepository rolesRepository        = new RolesRepository(context);

            Role role = rolesRepository.GetAll(filter: r => r.ID == model.ID, includeProperties: "AuthenticatingActions").FirstOrDefault();

            if (role == null)
            {
                return(HttpNotFound());
            }

            role.AuthenticatingActions = null;
            rolesRepository.Delete(role);

            return(RedirectToAction("Index", "Roles"));
        }
Example #13
0
        private void PopulateAssignedRoles(User user, RolesRepository rolesRepository)
        {
            var                    allRoles  = rolesRepository.GetAll();
            HashSet <int>          userRoles = new HashSet <int>(user.Roles.Select(r => r.ID));
            List <AssignedRolesVM> model     = new List <AssignedRolesVM>();

            foreach (var role in allRoles)
            {
                model.Add(new AssignedRolesVM
                {
                    ID         = role.ID,
                    Name       = role.Name,
                    IsAssigned = userRoles.Contains(role.ID)
                });
            }

            ViewBag.Roles = model;
        }
Example #14
0
        public ActionResult Index()
        {
            LibraryManagementSystemContext context = new LibraryManagementSystemContext();
            RolesRepository rolesRepository        = new RolesRepository(context);
            RolesIndexVM    model = new RolesIndexVM();

            this.TryUpdateModel(model);

            model.RolesPager             = model.RolesPager ?? new GenericPagerVM();
            model.RolesPager.CurrentPage = model.RolesPager.CurrentPage == 0 ? 1 : model.RolesPager.CurrentPage;

            model.RolesPager.PagesCount = this.GetPagesCount();
            model.RolesList             = rolesRepository.GetAll(model.RolesPager.CurrentPage, ApplicationConfiguration.ItemsPerPage);

            model.RolesPager.Prefix            = "RolesPager";
            model.RolesPager.Action            = "Index";
            model.RolesPager.Controller        = "Roles";
            model.RolesPager.CurrentParameters = new Dictionary <string, object>()
            {
                { "RolesPager.CurrentPage", model.RolesPager.CurrentPage }
            };

            return(View(model));
        }
        public ActionResult EditRole(int id)
        {
            LibraryManagementSystemContext context = new LibraryManagementSystemContext();
            RolesRepository rolesRepository = new RolesRepository(context);
            AuthenticatingActionsRepository authenticatingActionsRepository = new AuthenticatingActionsRepository(context);

            RolesEditRoleVM model = new RolesEditRoleVM();
            if (id > 0)
            {
                Role role = rolesRepository.GetAll(filter: r => r.ID == id, includeProperties: "AuthenticatingActions").FirstOrDefault();
                PopulateAssignedAuthenticatingActions(role, authenticatingActionsRepository);

                model.ID = role.ID;
                model.Name = role.Name;
            }
            else
            {
                Role role = new Role();
                role.AuthenticatingActions = new List<AuthenticatingAction>();
                PopulateAssignedAuthenticatingActions(role, authenticatingActionsRepository);
            }

            return View(model);
        }
        public ActionResult Index()
        {
            LibraryManagementSystemContext context = new LibraryManagementSystemContext();
            RolesRepository rolesRepository = new RolesRepository(context);
            RolesIndexVM model = new RolesIndexVM();
            this.TryUpdateModel(model);

            model.RolesPager = model.RolesPager ?? new GenericPagerVM();
            model.RolesPager.CurrentPage = model.RolesPager.CurrentPage == 0 ? 1 : model.RolesPager.CurrentPage;

            model.RolesPager.PagesCount = this.GetPagesCount();
            model.RolesList = rolesRepository.GetAll(model.RolesPager.CurrentPage, ApplicationConfiguration.ItemsPerPage);

            model.RolesPager.Prefix = "RolesPager";
            model.RolesPager.Action = "Index";
            model.RolesPager.Controller = "Roles";
            model.RolesPager.CurrentParameters = new Dictionary<string, object>()
            {
                {"RolesPager.CurrentPage",model.RolesPager.CurrentPage}
            };

            return View(model);
        }
        private void UpdateUserRoles(string[] assignedRoles, User user, RolesRepository rolesRepository)
        {
            if (assignedRoles == null)
            {
                return;
            }

            var assignedRolesHS = new HashSet<string>(assignedRoles);
            var userRoles = new HashSet<int>(user.Roles.Select(u => u.ID));

            foreach (var role in rolesRepository.GetAll())
            {
                if (assignedRolesHS.Contains(role.ID.ToString()))
                {
                    if (!userRoles.Contains(role.ID))
                    {
                        user.Roles.Add(role);
                    }
                }
                else if (userRoles.Contains(role.ID))
                {
                    user.Roles.Remove(role);
                }
            }
        }
        private void PopulateAssignedRoles(User user, RolesRepository rolesRepository)
        {
            var allRoles = rolesRepository.GetAll();
            HashSet<int> userRoles = new HashSet<int>(user.Roles.Select(r => r.ID));
            List<AssignedRolesVM> model = new List<AssignedRolesVM>();
            foreach (var role in allRoles)
            {
                model.Add(new AssignedRolesVM
                    {
                        ID = role.ID,
                        Name = role.Name,
                        IsAssigned = userRoles.Contains(role.ID)
                    });
            }

            ViewBag.Roles = model;
        }
        public ActionResult EditUser(UsersEditUserVM model, string[] assignedRoles)
        {
            LibraryManagementSystemContext context = new LibraryManagementSystemContext();

            ModelState.Remove("DateOut");

            UsersRepository usersRepositoryContext = new UsersRepository(context);
            if (model.Email != null && usersRepositoryContext.GetAll().Any(u => u.Email == model.Email) &&
                model.ID != usersRepositoryContext.GetAll(filter: u => u.Email == model.Email).FirstOrDefault().ID)
            {
                ModelState.AddModelError("Email", "* email already exists");
            }
            if (model.ID <= 0 && string.IsNullOrEmpty(model.Password))
            {
                this.ModelState.AddModelError("Password", "* password required");
            }
            if (!this.ModelState.IsValid)
            {
                RolesRepository rolesRepository = new RolesRepository(context);
                var allRoles = rolesRepository.GetAll();

                List<AssignedRolesVM> assignedRolesViewModel = new List<AssignedRolesVM>();
                foreach (var role in allRoles)
                {
                    assignedRolesViewModel.Add(new AssignedRolesVM
                        {
                            ID = role.ID,
                            Name = role.Name,
                            IsAssigned = false
                        });
                }

                ViewBag.Roles = assignedRolesViewModel;
                return View(model);
            }

            TryUpdateModel(model);

            User user = null;
            using (UnitOfWork unitOfWork = new UnitOfWork(context))
            {
                try
                {
                    UsersRepository usersRepositoryUnitOfWork = new UsersRepository(unitOfWork);
                    RolesRepository rolesRepository = new RolesRepository(unitOfWork);

                    if (model.ID > 0)
                    {
                        user = usersRepositoryUnitOfWork.GetAll(filter: u => u.ID == model.ID, includeProperties: "Roles").FirstOrDefault();
                        user.PersonalNumber = model.PersonalNumber;
                    }
                    else
                    {
                        user = new User();
                        user.Roles = new List<Role>();
                        user.PersonalNumber = usersRepositoryUnitOfWork.GetAll().LastOrDefault().PersonalNumber + 1;
                    }

                    user.Password =
                        (model.Password != null) && (model.Password.Trim() != String.Empty) ? model.Password.Trim() : user.Password;
                    user.FirstName = model.FirstName;
                    user.Email = model.Email;
                    user.Address = model.Address;
                    user.LastName = model.LastName;
                    user.Birthday = model.Birthday;
                    user.DateIn = model.DateIn;
                    user.DateOut = model.DateOut != null ? model.DateOut : null;

                    UpdateUserRoles(assignedRoles, user, rolesRepository);
                    usersRepositoryUnitOfWork.Save(user);

                    PopulateAssignedRoles(user, rolesRepository);
                    unitOfWork.Commit();
                }
                catch (Exception ex)
                {
                    unitOfWork.RollBack();
                    throw ex;
                }
            }

            return RedirectToAction("Index", "Users");
        }
Example #20
0
 public List <Role> GetAll()
 {
     return(rolesRep.GetAll());
 }
Example #21
0
        public ActionResult EditRole(RolesEditRoleVM model, string[] assignedAuthenticatingActions)
        {
            LibraryManagementSystemContext context = new LibraryManagementSystemContext();

            Role role = null;

            if (!this.ModelState.IsValid)
            {
                AuthenticatingActionsRepository authenticatingActionsRepository = new AuthenticatingActionsRepository(context);

                var authenticatingActions = authenticatingActionsRepository.GetAll();
                List <AuthenticatingActionsVM> authenticatingActionsViewModel = new List <AuthenticatingActionsVM>();

                foreach (var action in authenticatingActions)
                {
                    authenticatingActionsViewModel.Add(new AuthenticatingActionsVM
                    {
                        ID         = action.ID,
                        Name       = action.Name,
                        IsAssigned = false
                    });
                }

                ViewBag.AuthenticatingActions = authenticatingActionsViewModel;
                return(View(model));
            }

            using (UnitOfWork unitOfWork = new UnitOfWork(context))
            {
                try
                {
                    var authenticatingActionsRepository = new AuthenticatingActionsRepository(unitOfWork);

                    var rolesRepository = new RolesRepository(unitOfWork);
                    if (model.ID > 0)
                    {
                        role = rolesRepository.GetAll(filter: r => r.ID == model.ID, includeProperties: "AuthenticatingActions").FirstOrDefault();
                    }
                    else
                    {
                        role = new Role();
                        role.AuthenticatingActions = new List <AuthenticatingAction>();
                    }

                    role.Name = model.Name;

                    UpdateAuthenticatingActions(assignedAuthenticatingActions, role, authenticatingActionsRepository);
                    rolesRepository.Save(role);
                    PopulateAssignedAuthenticatingActions(role, authenticatingActionsRepository);

                    unitOfWork.Commit();
                }
                catch (Exception ex)
                {
                    unitOfWork.RollBack();
                    throw ex;
                }
            }

            return(RedirectToAction("Index", "Roles"));
        }
        public ActionResult EditRole(RolesEditRoleVM model, string[] assignedAuthenticatingActions)
        {
            LibraryManagementSystemContext context = new LibraryManagementSystemContext();

            Role role = null;
            if (!this.ModelState.IsValid)
            {
                AuthenticatingActionsRepository authenticatingActionsRepository = new AuthenticatingActionsRepository(context);

                var authenticatingActions = authenticatingActionsRepository.GetAll();
                List<AuthenticatingActionsVM> authenticatingActionsViewModel = new List<AuthenticatingActionsVM>();

                foreach (var action in authenticatingActions)
                {
                    authenticatingActionsViewModel.Add(new AuthenticatingActionsVM
                    {
                        ID = action.ID,
                        Name = action.Name,
                        IsAssigned = false
                    });
                }

                ViewBag.AuthenticatingActions = authenticatingActionsViewModel;
                return View(model);
            }

            using (UnitOfWork unitOfWork = new UnitOfWork(context))
            {
                try
                {
                    var authenticatingActionsRepository = new AuthenticatingActionsRepository(unitOfWork);

                    var rolesRepository = new RolesRepository(unitOfWork);
                    if (model.ID > 0)
                    {
                        role = rolesRepository.GetAll(filter: r => r.ID == model.ID, includeProperties: "AuthenticatingActions").FirstOrDefault();
                    }
                    else
                    {
                        role = new Role();
                        role.AuthenticatingActions = new List<AuthenticatingAction>();
                    }

                    role.Name = model.Name;

                    UpdateAuthenticatingActions(assignedAuthenticatingActions, role, authenticatingActionsRepository);
                    rolesRepository.Save(role);
                    PopulateAssignedAuthenticatingActions(role, authenticatingActionsRepository);

                    unitOfWork.Commit();
                }
                catch (Exception ex)
                {
                    unitOfWork.RollBack();
                    throw ex;
                }
            }

            return RedirectToAction("Index", "Roles");
        }
Example #23
0
        public ActionResult EditUser(UsersEditUserVM model, string[] assignedRoles)
        {
            LibraryManagementSystemContext context = new LibraryManagementSystemContext();

            ModelState.Remove("DateOut");

            UsersRepository usersRepositoryContext = new UsersRepository(context);

            if (model.Email != null && usersRepositoryContext.GetAll().Any(u => u.Email == model.Email) &&
                model.ID != usersRepositoryContext.GetAll(filter: u => u.Email == model.Email).FirstOrDefault().ID)
            {
                ModelState.AddModelError("Email", "* email already exists");
            }
            if (model.ID <= 0 && string.IsNullOrEmpty(model.Password))
            {
                this.ModelState.AddModelError("Password", "* password required");
            }
            if (!this.ModelState.IsValid)
            {
                RolesRepository rolesRepository = new RolesRepository(context);
                var             allRoles        = rolesRepository.GetAll();

                List <AssignedRolesVM> assignedRolesViewModel = new List <AssignedRolesVM>();
                foreach (var role in allRoles)
                {
                    assignedRolesViewModel.Add(new AssignedRolesVM
                    {
                        ID         = role.ID,
                        Name       = role.Name,
                        IsAssigned = false
                    });
                }

                ViewBag.Roles = assignedRolesViewModel;
                return(View(model));
            }

            TryUpdateModel(model);

            User user = null;

            using (UnitOfWork unitOfWork = new UnitOfWork(context))
            {
                try
                {
                    UsersRepository usersRepositoryUnitOfWork = new UsersRepository(unitOfWork);
                    RolesRepository rolesRepository           = new RolesRepository(unitOfWork);

                    if (model.ID > 0)
                    {
                        user = usersRepositoryUnitOfWork.GetAll(filter: u => u.ID == model.ID, includeProperties: "Roles").FirstOrDefault();
                        user.PersonalNumber = model.PersonalNumber;
                    }
                    else
                    {
                        user                = new User();
                        user.Roles          = new List <Role>();
                        user.PersonalNumber = usersRepositoryUnitOfWork.GetAll().LastOrDefault().PersonalNumber + 1;
                    }

                    user.Password =
                        (model.Password != null) && (model.Password.Trim() != String.Empty) ? model.Password.Trim() : user.Password;
                    user.FirstName = model.FirstName;
                    user.Email     = model.Email;
                    user.Address   = model.Address;
                    user.LastName  = model.LastName;
                    user.Birthday  = model.Birthday;
                    user.DateIn    = model.DateIn;
                    user.DateOut   = model.DateOut != null ? model.DateOut : null;

                    UpdateUserRoles(assignedRoles, user, rolesRepository);
                    usersRepositoryUnitOfWork.Save(user);

                    PopulateAssignedRoles(user, rolesRepository);
                    unitOfWork.Commit();
                }
                catch (Exception ex)
                {
                    unitOfWork.RollBack();
                    throw ex;
                }
            }

            return(RedirectToAction("Index", "Users"));
        }