public void TestAssignPermissionToRole()
        {
            //Arrange
            Permission permission = permissionService.GetPermissions().FirstOrDefault();

            //Act
            permissionService.AssignPermissionToRole("Reseller Admin", permission.PermissionId);

            //Assert
            Assert.AreEqual("Reseller Admin", permissionService.GetById(permission.PermissionId).Roles.Where(r => r.Name == "Reseller Admin").FirstOrDefault().Name);
            Assert.AreEqual(2, permissionService.GetById(permission.PermissionId).Roles.Count());
        }
Пример #2
0
        public Response AddPermissionToRole([FromBody] PermissionModel permissionModel)
        {
            Response reqResponse = new Response();

            if (permissionModel.username != "root")
            {
                reqResponse.SetResponse(401, "Not Authorized", "You are not authorized to add a user to a role!", null);
                goto Finish;
            }

            int userId = _userService.GetUser(permissionModel.username, permissionModel.password);

            if (userId == -1)
            {
                reqResponse.SetResponse(401, "Not Authorized", "Invalid credentials inserted!", null);
                goto Finish;
            }

            if (!_roleService.ExistsRole(permissionModel.roleName))
            {
                reqResponse.SetResponse(500, "Not Existing", "Role '" + permissionModel.roleName + "' does not exist in the system.", null);
                goto Finish;
            }

            if (!_permissionService.ExistsPermission(permissionModel.permissionName))
            {
                reqResponse.SetResponse(500, "Not Existing", "User '" + permissionModel.permissionName + "' does not exist in the system.", null);
                goto Finish;
            }

            if (_roleService.ExistsPermissionInRole(permissionModel.roleName, permissionModel.permissionName))
            {
                reqResponse.SetResponse(500, "Already existing", "Permission '" + permissionModel.permissionName + "' is already assigned to role '" + permissionModel.roleName + "'.", null);
                goto Finish;
            }

            _permissionService.AssignPermissionToRole(permissionModel.permissionName, permissionModel.roleName);
            reqResponse = new Response();

Finish:
            return(reqResponse);
        }