Beispiel #1
0
        public void TestUserRole()
        {
            string permName1 = Guid.NewGuid().ToString();
            long   permId1   = permService.AddPermission(permName1, permName1);
            string permName2 = Guid.NewGuid().ToString();
            long   permId2   = permService.AddPermission(permName2, permName2);


            string roleName1 = Guid.NewGuid().ToString();
            long   roleId1   = roleService.AddNew(roleName1);

            string roleName2 = Guid.NewGuid().ToString();
            long   roleId2   = roleService.AddNew(roleName2);

            string userPhone = "178158";

            userId = userService.AddAdminUser("aaa", userPhone, "123", "*****@*****.**", null);

            roleService.AddRoleIds(userId, new long[] { roleId1 });
            permService.AddPermIds(roleId1, new long[] { permId1 });
            Assert.IsTrue(userService.HasPermission(userId, permName1));
            //Assert.IsFalse(userService.HasPermission(userId, permName2));

            roleService.UpdateRoleIds(userId, new long[] { roleId2 });
            Assert.IsFalse(userService.HasPermission(userId, permName1));
            CollectionAssert.AreEqual(roleService.GetByAdminUserId(userId).Select(r => r.Id).ToArray(),
                                      new long[] { roleId2 });
        }
Beispiel #2
0
        public void TestHasPerm()
        {
            try
            {
                PermissionService permService = new PermissionService();
                string            permName1   = Guid.NewGuid().ToString();
                long   permId1   = permService.AddPermission(permName1, permName1);
                string permName2 = Guid.NewGuid().ToString();
                long   permId2   = permService.AddPermission(permName2, permName2);

                RoleService roleService = new RoleService();
                string      roleName    = Guid.NewGuid().ToString();
                long        roleId      = roleService.AddNew(roleName);

                string userPhone = "136138";
                long   userId    = new AdminUserService().AddAdminUser("aaa", userPhone, "123", "*****@*****.**", null);

                roleService.AddRoleIds(userId, new long[] { roleId });
                permService.AddPermIds(roleId, new long[] { permId1 });

                Assert.IsTrue(service.HasPermission(userId, permName1));
                Assert.IsFalse(service.HasPermission(userId, permName2));

                service.MarkDeleted(userId);
            }
            catch (DbEntityValidationException ex)
            {
                foreach (var item in ex.EntityValidationErrors.SelectMany(err => err.ValidationErrors))
                {
                    Console.WriteLine(item.ErrorMessage);
                }
            }
        }
Beispiel #3
0
        public void TestUserRole()
        {
            //权限项1
            string permName1 = Guid.NewGuid().ToString();
            long   permId1   = permissionService.AddPermission(permName1, permName1);
            //权限项2
            string permName2 = Guid.NewGuid().ToString();
            long   permId2   = permissionService.AddPermission(permName2, permName2);
            //用户
            string phoneNum = Guid.NewGuid().ToString().Substring(0, 11);

            adminUserId = adminUserService.AddAdminUser("wangwu", phoneNum, "123", "*****@*****.**", null);
            //角色1,2
            string roleName1 = Guid.NewGuid().ToString();
            string roleName2 = Guid.NewGuid().ToString();
            long   roleId1   = roleService.AddNew(roleName1);
            long   roleId2   = roleService.AddNew(roleName2);

            //给角色1添加权限项1
            permissionService.AddPermIds(roleId1, new long[] { permId1 });
            //给角色2添加权限项2
            permissionService.AddPermIds(roleId2, new long[] { permId2 });
            //给用户adminuser添加角色1
            roleService.AddRoleIds(adminUserId, new long[] { roleId1 });

            Assert.IsTrue(adminUserService.HasPermission(adminUserId, permName1));
            Assert.IsFalse(adminUserService.HasPermission(adminUserId, permName2));

            roleService.UpdateRoleIds(adminUserId, new long[] { roleId2 });

            Assert.IsFalse(adminUserService.HasPermission(adminUserId, permName1));
            Assert.IsTrue(adminUserService.HasPermission(adminUserId, permName2));
            CollectionAssert.AreEqual(roleService.GetByAdminUserId(adminUserId).Select(a => a.Id).ToArray(), new long[] { roleId2 });
        }
Beispiel #4
0
        public void HasPermission()
        {
            PermissionService permissionService = new PermissionService();
            //权限项1
            string permName1 = Guid.NewGuid().ToString();
            long   permId1   = permissionService.AddPermission(permName1, permName1);
            //权限项2
            string permName2 = Guid.NewGuid().ToString();
            long   permId2   = permissionService.AddPermission(permName2, permName2);
            //用户
            string phoneNum    = Guid.NewGuid().ToString().Substring(0, 11);
            long   adminUserId = adminUserService.AddAdminUser("李四", phoneNum, "123", "*****@*****.**", null);
            //角色1,2
            RoleService roleService = new RoleService();
            string      roleName1   = Guid.NewGuid().ToString();
            string      roleName2   = Guid.NewGuid().ToString();
            long        roleId1     = roleService.AddNew(roleName1);
            long        roleId2     = roleService.AddNew(roleName2);

            //给角色1添加权限项1
            permissionService.AddPermIds(roleId1, new long[] { permId1 });
            //给用户adminuser添加角色1
            roleService.AddRoleIds(adminUserId, new long[] { roleId1 });

            Assert.IsTrue(adminUserService.HasPermission(adminUserId, permName1));
            Assert.IsFalse(adminUserService.HasPermission(adminUserId, permName2));
        }
Beispiel #5
0
        public async Task AddPermissionReturnsCorrectType()
        {
            var newPermission = ModelFakes.PermissionFake.Generate();

            newPermission.UserId = _userWithNoPermissions.UserId;

            var returnPermission = await _testPermissionService.AddPermission(newPermission);

            returnPermission.Should().BeOfType <Permission>();
        }
Beispiel #6
0
        private async Task <dynamic> AddPermission()
        {
            var permissionApiModel = this.Bind <PermissionApiModel>(binderIgnore => binderIgnore.Id,
                                                                    binderIgnore => binderIgnore.CreatedBy,
                                                                    binderIgnore => binderIgnore.CreatedDateTimeUtc,
                                                                    binderIgnore => binderIgnore.ModifiedDateTimeUtc,
                                                                    binderIgnore => binderIgnore.ModifiedBy);

            var incomingPermission = permissionApiModel.ToPermissionDomainModel();

            Validate(incomingPermission);
            await CheckWriteAccess(_clientService, _grainService, permissionApiModel.Grain, permissionApiModel.SecurableItem);

            var permission = await _permissionService.AddPermission(incomingPermission);

            return(CreateSuccessfulPostResponse(permission.ToPermissionApiModel()));
        }
Beispiel #7
0
        public void PermissionTest()
        {
            PermissionService permService = new PermissionService();
            AdminUserService  userService = new AdminUserService();
            RoleService       roleService = new RoleService();
            string            roleName1   = Guid.NewGuid().ToString();
            string            permName1   = Guid.NewGuid().ToString();
            long   roleId1   = roleService.AddNew(roleName1);
            long   permId    = permService.AddPermission(permName1, permName1);
            string userPhone = "139138";
            long   userId    = userService.AddAdminUser("Aaa", userPhone, "mima", "email", null);

            roleService.AddRoleIds(userId, new long[] { roleId1 });
            permService.AddPermIds(roleId1, new long[] { permId });
            Assert.IsTrue(userService.HasPermission(userId, permName1));
            Assert.IsFalse(userService.HasPermission(userId, "1"));
            userService.MarkDeleted(userId);
        }
Beispiel #8
0
        private async Task AddDefaultRoleAndPermissionAsync(Client client)
        {
            try
            {
                var newPermission = await _permissionService.AddPermission(new Permission
                {
                    Name          = Domain.Defaults.Authorization.ManageAuthorizationPermissionName,
                    Grain         = Domain.Defaults.Authorization.AppGrain,
                    SecurableItem = client.TopLevelSecurableItem.Name
                });

                try
                {
                    await _roleService.AddRole(new Role
                    {
                        Name          = $"{client.Id}-admin",
                        Grain         = Domain.Defaults.Authorization.AppGrain,
                        SecurableItem = client.TopLevelSecurableItem.Name,
                        Permissions   = new List <Permission> {
                            newPermission
                        }
                    });
                }
                catch (Exception)
                {
                    //if we can't create the role, delete the client and the permission
                    await _clientService.DeleteClient(client);

                    await _permissionService.DeletePermission(newPermission);

                    throw;
                }
            }
            catch (Exception)
            {
                //if we can't save the permission, delete the client and rethrow the exception
                await _clientService.DeleteClient(client);

                throw;
            }
        }
Beispiel #9
0
        public async Task PermissionService_AddPermission_SuccessfulAsync()
        {
            var mockPermissionStore = new Mock <IPermissionStore>()
                                      .SetupAddPermissions(new List <Permission>())
                                      .Create();

            var mockRoleStore = new Mock <IRoleStore>().Object;

            var permissionService = new PermissionService(
                mockPermissionStore,
                new Mock <RoleService>(mockRoleStore, mockPermissionStore).Object);

            var permission = await permissionService.AddPermission(new Permission
            {
                Grain         = "app",
                SecurableItem = "patientsafety",
                Name          = "manageusers"
            });

            Assert.NotNull(permission);
        }
Beispiel #10
0
 public IHttpActionResult AddPermission(Permission permission)
 {
     try
     {
         if (permission != null)
         {
             if (ModelState.IsValid)
             {
                 string accessToken       = Request.Headers.Authorization == null ? null : Request.Headers.Authorization.Parameter;
                 bool   isPermissionSaved = _service.AddPermission(permission, accessToken);
                 if (isPermissionSaved)
                 {
                     return(Content(HttpStatusCode.OK, "Permission has been created successfully."));
                 }
                 else
                 {
                     return(Content(HttpStatusCode.OK, "PermissionName is already exist."));
                 }
             }
             else
             {
                 string modelError = null;
                 for (int i = 0; i < ModelState.Keys.Count; i++)
                 {
                     modelError += ModelState.Values.ToList()[i].Errors[0].ErrorMessage + ",";
                 }
                 return(Content(HttpStatusCode.BadRequest, modelError));
             }
         }
         else
         {
             throw new Exception("Please provide the permission details.");
         }
     }
     catch (Exception ex)
     {
         Logger.WriteException("AddPermission : ", ex);
         return(Content(HttpStatusCode.BadRequest, ex.GetBaseException().Message));
     }
 }
Beispiel #11
0
 static void Main(string[] args)
 {
     PermissionService.AddPermission();
 }