Beispiel #1
0
        public async Task <IActionResult> PutRole(string id, [FromBody] RoleVm roleVm)
        {
            if (id != roleVm.Id)
            {
                return(BadRequest());
            }

            var role = await _roleManager.FindByIdAsync(id);

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

            role.Name           = roleVm.Name;
            role.NormalizedName = roleVm.Name.ToUpper();

            var result = await _roleManager.UpdateAsync(role);

            if (result.Succeeded)
            {
                return(NoContent());
            }
            return(BadRequest(result.Errors));
        }
        public async Task <IActionResult> Create(RoleVm vm)
        {
            IdentityRole identityRole = new IdentityRole
            {
                Name = vm.Name,
            };

            if (ModelState.IsValid)
            {
                IdentityResult result = await manager.CreateAsync(identityRole);

                if (result.Succeeded)
                {
                    return(RedirectToAction("Index"));
                }
                if (result.Errors.Count() > 0)
                {
                    foreach (var s in result.Errors)
                    {
                        ModelState.AddModelError("", s.ToString());
                    }
                }
            }
            return(View(identityRole));
        }
        public async Task <IActionResult> DeleteRole(string id)
        {
            //// GET ROLE WITH ID (KEY)
            var role = await _roleManger.FindByIdAsync(id);

            //// IF KEY IS NOT EXIST (ROLE IS NULL), RETURN STATUS 404
            if (role == null)
            {
                return(NotFound(new ApiNotFoundResponse($"Cannot find role with id: {id}")));
            }

            //// DELETE ROLE FROM DATATABLE IN DATABASE AND SAVE CHANGE
            var result = await _roleManger.DeleteAsync(role);

            //// IF RESULT AFTER DELETE IS SUCCEEDED, DELETE SUCCESS AND RETURN STATUS 200, ELSE RETURN STATUS 400
            if (result.Succeeded)
            {
                var roleVm = new RoleVm()
                {
                    Id   = role.Id,
                    Name = role.Name
                };
                return(Ok(roleVm));
            }

            return(BadRequest(new ApiBadRequestResponse(result)));
        }
 public static void UpdateRole(this Role role, RoleVm roleVm)
 {
     role.Id          = roleVm.Id;
     role.Name        = roleVm.Name;
     role.Description = roleVm.Description;
     role.CreatedDate = roleVm.CreatedDate;
     role.CreatedBy   = roleVm.CreatedBy;
 }
        public async Task <JsonResult> GetRoleList(RoleVm model)
        {
            var result            = new SearchResult <List <SystemRole> >();
            var respositoryResult = await RoleRespository.GetList(model, UserToken);

            result.Status = ResultConfig.Ok;
            result.Info   = ResultConfig.SuccessfulMessage;
            result.Rows   = respositoryResult.Item2;
            result.Total  = respositoryResult.Item1;
            return(Json(result));
        }
Beispiel #6
0
        public async Task <IActionResult> Post([FromBody] RoleVm roleVm)
        {
            if (ModelState.IsValid)
            {
                IdentityRole <long> identityRole = new IdentityRole <long>
                {
                    Name = roleVm.Name
                };
                await _roleManager.CreateAsync(identityRole);

                return(Ok("Created."));
            }
            return(BadRequest());
        }
Beispiel #7
0
 public ActionResult Edit(RoleVm roleVm)
 {
     if (ModelState.IsValid)
     {
         var role = _roleService.GetSingleById(roleVm.Id);
         role.UpdateRole(roleVm);
         _roleService.Update(role);
         _roleService.Save();
         TempData["MessageSuccess"] = Message.EditSuccess;
         return(RedirectToAction("Index"));
     }
     TempData["MessageError"] = Message.EditError;
     return(View());
 }
Beispiel #8
0
 public IActionResult AddRole([FromBody] RoleVm r)
 {
     if (r != null)
     {
         Role role = new Role();
         role.RoleName    = r.RoleName;
         role.Description = r.Description;
         _service.AddRole(role);
         return(new OkObjectResult(r));
     }
     else
     {
         return(NotFound());
     }
 }
        public async Task <IActionResult> GetById(string id)
        {
            var role = await _roleManager.FindByIdAsync(id);

            if (role == null)
            {
                return(NotFound());
            }
            var roleVM = new RoleVm()
            {
                Id   = role.Id,
                Name = role.Name
            };

            return(Ok(role));
        }
        // GET: Role

        public ActionResult Index(int page = 1, int pageSize = 5)
        {
            User   user     = GetUserSession();
            string userType = user.Role.roleName;

            ViewBag.userType = userType;
            //Show list of roles and list in descending order of the role id
            var roleList = _roleService.GetAllRoles().OrderBy(p => p.rId).OrderByDescending(p => p.rId);
            //mapping with the RoleViewModel to show the pagination on the view page
            var roleViewList = AutoMapper.Mapper.Map <IEnumerable <Role>, IEnumerable <RoleViewModel> >(roleList);
            var model        = new RoleVm();

            //Configure to show only  5 categories per page
            model.Roles = roleViewList.ToPagedList(page, pageSize);
            return(View(model));
        }
Beispiel #11
0
        public async Task <IActionResult> Put(int id, [FromBody] RoleVm roleVm)
        {
            try
            {
                IdentityRole <long> identityRole = await _roleManager.FindByIdAsync(id.ToString());

                identityRole.Name = roleVm.Name;
                await _roleManager.UpdateNormalizedRoleNameAsync(identityRole);

                return(Ok("role updated."));
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Beispiel #12
0
        public ActionResult Create(RoleVm roleVm)
        {
            if (ModelState.IsValid)
            {
                var role = new Role();
                roleVm.CreatedBy   = 1;
                roleVm.CreatedDate = DateTime.Now;
                role.UpdateRole(roleVm);
                _roleService.Add(role);
                _roleService.Save();

                TempData["MessageSuccess"] = Message.CreateSuccess;
                return(RedirectToAction("Index"));
            }
            TempData["MessageError"] = Message.CreateError;
            return(View());
        }
Beispiel #13
0
        public async Task <IActionResult> GetById(string id)
        {
            var role = await _roleManager.FindByIdAsync(id);

            if (role == null)
            {
                return(NotFound(new ApiNotFoundResponse($"Cannot find role with id: {id}")));
            }

            var roleVm = new RoleVm()
            {
                Id   = role.Id,
                Name = role.Name,
            };

            return(Ok(roleVm));
        }
Beispiel #14
0
        public IActionResult ListRoles()
        {
            var roles = _roleManager.Roles.ToList();

            List <RoleVm> rolesVmList = new List <RoleVm>();

            foreach (var role in roles)
            {
                var roleVm = new RoleVm
                {
                    Name = role.Name
                };

                rolesVmList.Add(roleVm);
            }

            return(View(rolesVmList));
        }
Beispiel #15
0
        public async Task <IActionResult> CreateRole(RoleVm roleVm)
        {
            if (ModelState.IsValid)
            {
                IdentityRole role = new IdentityRole
                {
                    Name = roleVm.Name
                };

                var result = await _roleManager.CreateAsync(role);

                if (result.Succeeded)
                {
                    return(RedirectToAction("Index", "Employees"));
                }
            }
            return(View(roleVm));
        }
        public async Task <IActionResult> PostRole(RoleVm roleVm)
        {
            var role = new IdentityRole()
            {
                Id             = roleVm.Id,
                Name           = roleVm.Name,
                NormalizedName = roleVm.Name.ToUpper()
            };
            var result = await _roleManager.CreateAsync(role);

            if (result.Succeeded)
            {
                return(CreatedAtAction(nameof(GetById), new { id = role.Id }, roleVm));
            }
            else
            {
                return(BadRequest(result.Errors));
            }
        }
        public async Task <IActionResult> GetById(string id)
        {
            //// GET ROLE WITH ID (KEY)
            var role = await _roleManger.FindByIdAsync(id);

            //// IF KEY IS NOT EXIST (ROLE IS NULL), RETURN STATUS 404
            if (role == null)
            {
                return(NotFound(new ApiNotFoundResponse($"Cannot find role with id: {id}")));
            }

            //// GIVE INFO TO RoleVm (JUST SHOW NEEDED FIELD)
            var roleVm = new RoleVm()
            {
                Id   = role.Id,
                Name = role.Name,
            };

            return(Ok(roleVm));
        }
Beispiel #18
0
 public ActionResult Create(RoleVm roles)
 {
     try
     {
         var role = new IdentityRole
         {
             Name = roles.Name
         };
         db.Roles.Add(role);
         if (db.SaveChanges() > 0)
         {
             return(RedirectToAction("Index"));
         }
     }
     catch (Exception ex)
     {
         ViewBag.result = ex.Message;
     }
     return(View(roles));
 }
Beispiel #19
0
        public RoleVm Save(long userId, RoleVm toSave)
        {
            var role = toSave.Role;

            PreSave(userId, ref role, toSave.ActionMode);
            toSave.Role = role;

            switch (toSave.ActionMode)
            {
            case Enumerations.ActionMode.Add:
                toSave.Role = Create(userId, toSave.Role);
                break;

            case Enumerations.ActionMode.Edit:
                toSave.Role = Edit(userId, toSave.Role, toSave.RoleUsers, toSave.RolePermissions);
                break;
            }

            return(Init(userId, toSave.Role.Id));
        }
        public async Task <IActionResult> DeleteRole(string id)
        {
            var role = await _roleManager.FindByIdAsync(id);

            if (role == null)
            {
                return(NotFound());
            }
            var result = await _roleManager.DeleteAsync(role);

            if (result.Succeeded)
            {
                var roleVm = new RoleVm()
                {
                    Id   = role.Id,
                    Name = role.Name
                };
                return(Ok(roleVm));
            }
            return(BadRequest(result.Errors));
        }
Beispiel #21
0
        public async Task <IActionResult> DeleteRole(string id)
        {
            var role = await _roleManager.FindByIdAsync(id);

            if (role == null)
            {
                return(NotFound(new ApiNotFoundResponse($"Cannot find role with id: {id}")));
            }

            var result = await _roleManager.DeleteAsync(role);

            if (result.Succeeded)
            {
                var rolevm = new RoleVm()
                {
                    Id   = role.Id,
                    Name = role.Name
                };
                return(Ok(rolevm));
            }
            return(BadRequest(new ApiBadRequestResponse(result)));
        }
Beispiel #22
0
        public RoleVm Init(long userId, long?id)
        {
            var toRet = new RoleVm
            {
                ParentRoles = LoadAll(userId).ToDictionary(k => k.Id.ToUiString(), v => v.FullParent + (v.FullParent == "" ? "" : "-") + v.Code),
                ActionMode  = Enumerations.ActionMode.Add,
                Role        = new Role {
                    ParentRole = new Role()
                }
            };

            if (id != null)
            {
                var role = LoadSingle(userId, Convert.ToInt64(id));
                toRet.Role            = role;
                toRet.ActionMode      = Enumerations.ActionMode.Edit;
                toRet.RoleUsers       = GetRoleUsers(userId, toRet.Role.Id);
                toRet.RolePermissions = GetRolePermissions(userId, toRet.Role.Id);
            }

            return(toRet);
        }
Beispiel #23
0
        /// <summary>
        /// Role creation method
        /// </summary>
        /// <param name = "data"> Data </param>
        /// <returns> </returns>
        public RoleVm Create(RoleSubmitM data)
        {
            if (_context.Roles.Any(r => r.Name == data.Name))
            {
                throw new Exception(nameof(Resource.NameError));
            }

            var role = new Role
            {
                Name        = data.Name,
                Description = data.Description,
                AccessToSystemFunctionsArray = data.AccessToSystemFunctions,
                StandardDataAccessArray      = data.StandardDataAccess.ToPermissionsModel(),
                NormalizedName = data.Name.ToUpper(),
                Status         = RoleStatuses.Active,
                SqlWalletUser  = data.SqlWalletUser
            };

            _context.Roles.Add(role);
            _context.SaveChanges();

            return(RoleVm.Create(role));
        }
Beispiel #24
0
        /// <summary>
        /// 获取角色列表
        /// </summary>
        /// <returns></returns>
        public async Task <Tuple <long, List <SystemRole> > > GetList(RoleVm model, Token userToken)
        {
            if (model == null)
            {
                return(new Tuple <long, List <SystemRole> >(0, new List <SystemRole>()));
            }

            var totalQuery = this.Entity.Where(r => r.IsActive);
            var listQuery  = this.Entity.Where(r => r.IsActive);

            if (!string.IsNullOrEmpty(model.RoleName))
            {
                totalQuery = totalQuery.Where(r => r.RoleName.Contains(model.RoleName));
                listQuery  = listQuery.Where(r => r.RoleName.Contains(model.RoleName));
            }
            if (!string.IsNullOrEmpty(model.CreateUser))
            {
                totalQuery = totalQuery.Where(r => r.CreateUser.Contains(model.CreateUser));
                listQuery  = listQuery.Where(r => r.CreateUser.Contains(model.CreateUser));
            }
            if (!GlobalSetting.GoldList.Contains(userToken.Eid))
            {
                totalQuery = totalQuery.Where(r => r.CreateUser.Equals(userToken.Eid) || r.CreateRoleTid.Equals(userToken.RoleTid));
                listQuery  = listQuery.Where(r => r.CreateUser.Equals(userToken.Eid) || r.CreateRoleTid.Equals(userToken.RoleTid));
            }

            var total = totalQuery.CountAsync();

            var roleList = await listQuery.DynamicOrderBy(string.IsNullOrEmpty(model.OrderBy)? "DataChangeLastTime" : model.OrderBy,
                                                          model.OrderSequence)
                           .Skip((model.PageIndex - 1) * model.PageSize)
                           .Take(model.PageSize)
                           .ToListAsync();

            return(new Tuple <long, List <SystemRole> >(await total, roleList));
        }
Beispiel #25
0
        // Create Role
        public async Task <IActionResult> CreateRole(RoleVm vm)
        {
            await _roleManager.CreateAsync(new IdentityRole { Name = vm.Name });

            return(RedirectToAction("Index"));
        }
Beispiel #26
0
        /// <summary>
        /// Method for getting the role
        /// </summary>
        /// <param name = "id"> Id </param>
        /// <returns> </returns>
        public RoleVm GetRoleVmById(string id)
        {
            var role = GetRoleById(id);

            return(RoleVm.Create(role));
        }