public async Task UserRoleStore_create_user_add_to_role()
        {
            var user     = _userTestFactory.CreateUser();
            var username = "******";
            await _userStore.SetUserNameAsync(user, username, default(CancellationToken));

            await _userStore.SetNormalizedUserNameAsync(user, username, default(CancellationToken));

            await _userStore.CreateAsync(user, default(CancellationToken));

            var foundUser = await _userStore.FindByIdAsync(user.Id, default(CancellationToken));

            foundUser.UserName.ShouldMatch(user.UserName);

            var role = _roleTestFactory.CreateRole();
            await _roleStore.CreateAsync(role, default(CancellationToken));

            await _roleStore.SetNormalizedRoleNameAsync(role, role.Name, default(CancellationToken));

            await _userRoleStore.AddToRoleAsync(user, role.Name, default(CancellationToken));

            var roles = await _userRoleStore.GetRolesAsync(user, default(CancellationToken));

            roles.Count.ShouldBeGreaterThan(0);

            var users = await _userRoleStore.GetUsersInRoleAsync(role.Name, default(CancellationToken));

            users.Count.ShouldBeGreaterThan(0);

            var firstUser = users.FirstOrDefault();

            firstUser.ShouldNotBeNull();
            firstUser.UserName.ShouldMatch(user.UserName);
        }
Esempio n. 2
0
        public async Task RoleStore_create_find_by_id()
        {
            var role = _roleTestFactory.CreateRole();
            await _roleStore.CreateAsync(role, default(CancellationToken));

            await _roleStore.SetNormalizedRoleNameAsync(role, role.Name, default(CancellationToken));

            var fetchedRole = await _roleStore.FindByIdAsync(role.Id, default(CancellationToken));

            role.Name.ShouldMatch(fetchedRole.Name);
        }
Esempio n. 3
0
        public async Task <BaseResult> Handle(AddRoleCommand request, CancellationToken cancellationToken)
        {
            var newRole       = _mapper.Map <ApplicationRole>(request);
            var newRoleResult = await _rolesRepository.CreateAsync(newRole, cancellationToken);

            if (newRoleResult.Succeeded)
            {
                return(new BaseResult());
            }
            else
            {
                return(new BaseResult(success: false));
            }
        }
Esempio n. 4
0
        public async Task SeedAsync()
        {
            var cts = new CancellationTokenSource();

            var role = new IdentityRole("Admin");
            await mRoleStore.CreateAsync(role, cts.Token);

            var user = new ApiUser {
                Id = "mike", UserName = "******"
            };

            user.PasswordHash = mHasher.HashPassword(user, "123");

            await mUserStore.CreateAsync(user, cts.Token);
        }
Esempio n. 5
0
        public async Task <IActionResult> Post([FromBody] string model) //Create user
        {
            var result = await _roleStore.CreateAsync(new Role()
            {
                Name = "User"
            }, new CancellationTokenSource().Token);

            if (result.Succeeded)
            {
                return(new CreatedResult("role", new { model }));
            }
            else
            {
                return(new BadRequestObjectResult(result.Errors));
            }
        }
Esempio n. 6
0
        private void TestCrudApp(IApplicationBuilder app)
        {
            using (IServiceScope serviceScope = app.ApplicationServices.GetService <IServiceScopeFactory>().CreateScope()) {
                IClientStore     clientStore     = serviceScope.ServiceProvider.GetRequiredService <IClientStore>();
                IPermissionStore permissionStore = serviceScope.ServiceProvider.GetRequiredService <IPermissionStore>();
                IRoleStore       roleStore       = serviceScope.ServiceProvider.GetRequiredService <IRoleStore>();

                String clientId      = "test";
                Int32  client_Id     = -1;
                Int32  policy_Id     = -1;
                String permissionId  = "PermissionId01";
                Int32  permission_Id = -1;
                String RoleId        = "Role01";
                Int32  role_Id       = -1;


                PolicyServer1.Models.Client            existsingClient     = null;
                PolicyServer1.Models.PolicyPermission  existsingPermission = null;
                List <PolicyServer1.Models.Permission> permissions         = new List <PolicyServer1.Models.Permission>();
                PolicyServer1.Models.PolicyRole        existsingRole       = null;


                Task.Run(async() => {
                    try {
                        await clientStore.RemoveClientIdAsync(clientId);
                    } catch (Exception) { }

                    PolicyServer1.Models.Client newClient = new PolicyServer1.Models.Client {
                        ClientId    = clientId,
                        ClientName  = "ceci est un test",
                        Description = "ceci est une description",
                    };

                    client_Id = await clientStore.CreateAsync(newClient);

                    existsingClient             = await clientStore.GetFromClientIdAsync(clientId);
                    existsingClient.Description = "une autre descripiton";
                    existsingClient.ClientUri   = "http://localhost:50022";

                    await clientStore.UpdateAsync(existsingClient.Id, existsingClient);

                    policy_Id = existsingClient.Policy.Id;
                }).Wait();

                Task.Run(async() => {
                    try {
                        //await permissionStore.RemoveAsync((clientId, permissionId));
                    } catch (Exception) { }

                    PolicyServer1.Models.PolicyPermission newClientPermission = new PolicyServer1.Models.PolicyPermission {
                        PolicyId    = policy_Id,
                        Name        = permissionId,
                        Description = "Cecei un un desdcription"
                    };

                    permission_Id = await permissionStore.CreateAsync(newClientPermission);

                    existsingPermission             = await permissionStore.GetAsync((policy_Id, permission_Id));
                    existsingPermission.Description = "L'action de permission - 01";
                    existsingPermission.IsRevoked   = false;

                    await permissionStore.UpdateAsync((client_Id, permission_Id), existsingPermission);

                    if ((await permissionStore.GetByNameAsync(policy_Id, "Permission_0")) == null)
                    {
                        for (Int32 i = 0; i < 20; ++i)
                        {
                            await permissionStore.CreateAsync(new PolicyServer1.Models.PolicyPermission {
                                PolicyId    = policy_Id,
                                Name        = "Permission_" + i.ToString(),
                                Description = "Description_" + i.ToString(),
                            });
                        }

                        permissions = (await clientStore.GetAsync(existsingClient.Id)).Policy.Permissions;
                    }
                }).Wait();

                Task.Run(async() => {
                    try {
                        //await permissionStore.RemoveAsync((clientId, permissionId));
                    } catch (Exception) { }

                    PolicyServer1.Models.PolicyRole newClientRole = new PolicyServer1.Models.PolicyRole {
                        PolicyId      = policy_Id,
                        Name          = RoleId,
                        Description   = "Cecei un un desdcription",
                        IdentityRoles =
                        {
                            "id-role-01",
                            "id-role-02"
                        },
                        Subjects =
                        {
                            "Stella",
                            "Alice",
                            "Bob"
                        },
                        Permissions =
                        {
                            existsingPermission
                        }
                    };

                    role_Id = await roleStore.CreateAsync(newClientRole);

                    existsingRole             = await roleStore.GetAsync((policy_Id, role_Id));
                    existsingRole.Description = "Le role de permission - 01";
                    existsingRole.IdentityRoles.Remove("id-role-01");
                    existsingRole.Subjects.Remove("Stella");
                    existsingRole.Permissions.Add(permissions.Skip(5).First());
                    existsingRole.Permissions.Add(permissions.Skip(6).First());
                    existsingRole.Permissions.Add(permissions.Skip(7).First());
                    existsingRole.Permissions.Add(permissions.Skip(8).First());
                    //existsingRole.Permissions.Add(permissions.Skip(7).First());

                    await roleStore.UpdateAsync((policy_Id, role_Id), existsingRole);

                    existsingRole.Permissions.RemoveAt(0);

                    await roleStore.UpdateAsync((policy_Id, role_Id), existsingRole);

                    existsingRole.Permissions.First().IsRevoked = true;

                    await roleStore.UpdateAsync((policy_Id, role_Id), existsingRole);
                }).Wait();

                Task.Run(async() => {
                    PolicyServer1.Models.PolicyRole newClientRole = new PolicyServer1.Models.PolicyRole {
                        PolicyId      = policy_Id,
                        Name          = "childRole01",
                        Description   = "Cecei un un desdcription",
                        IdentityRoles =
                        {
                            "id-role-02"
                        },
                        Subjects =
                        {
                            "Bob"
                        },
                        Permissions =
                        {
                            permissions.Skip(10).First()
                        },
                        Parents =
                        {
                            existsingRole
                        }
                    };

                    newClientRole.Id = await roleStore.CreateAsync(newClientRole);

                    PolicyServer1.Models.PolicyRole newClientRole2 = new PolicyServer1.Models.PolicyRole {
                        PolicyId      = policy_Id,
                        Name          = "childRole02",
                        Description   = "Cecei un un desdcription",
                        IdentityRoles =
                        {
                            "id-role-02"
                        },
                        Subjects =
                        {
                            "Bob"
                        },
                        Permissions =
                        {
                            permissions.Skip(10).First()
                        },
                        Parents =
                        {
                            newClientRole
                        }
                    };

                    newClientRole2.Id = await roleStore.CreateAsync(newClientRole2);

                    PolicyServer1.Models.PolicyRole testChildRole02 = await roleStore.GetAsync((policy_Id, newClientRole2.Id));
                }).Wait();

                Task.Run(async() => {
                    PolicyServer1.Models.PolicyRole test = await roleStore.GetAsync((policy_Id, existsingRole.Id + 1));
                }).Wait();
            }
        }
Esempio n. 7
0
 public async Task <IdentityResult> CreateAsync(ApplicationRole role, CancellationToken cancellationToken)
 {
     return(await roleStore.CreateAsync(role, cancellationToken));
 }