async Task IIamProvider.AddRole(string policyName, string roleName, IIamProviderCache cache) { var roles = cache.GetRoles(policyName); if (roles == null || !roles.Contains(roleName)) { var policyId = await CreateOrGetPolicy(policyName); var role = await _roleManager.FindByNameAsync(roleName); if (role != null) { if (!(await _context.IamPolicyRoles.AnyAsync(x => x.PolicyId.Equals(policyId) && x.RoleId.Equals(role.Id)))) { var policyRole = new Model.PolicyRole <TKey>() { PolicyId = policyId, RoleId = role.Id }; _context.IamPolicyRoles.Add(policyRole); await _context.SaveChangesAsync(); cache.AddRole(policyName, roleName); } } } }
async Task <ICollection <string> > IIamProvider.GetRequiredRoles(string policyName, IIamProviderCache cache) { ICollection <string> ret = cache.GetRoles(policyName); if (ret == null || ret.Count == 0) { var policyId = await CreateOrGetPolicy(policyName); var roles = await _context.IamPolicyRoles .AsNoTracking() .Where(x => x.PolicyId.Equals(policyId)) .Select(x => x.RoleId) .ToListAsync(); ret = await _context.Roles .AsNoTracking() .Where(x => roles.Contains(x.Id)) .Select(x => x.Name) .ToListAsync(); foreach (var role in ret) { cache.AddRole(policyName, role); } } return(ret); }
Task IIamProvider.AddRole(string policyName, string roleName, IIamProviderCache cache) { cache.AddRole(policyName, roleName); return(Task.CompletedTask); }
public void AddRoleTest() { cache.AddRole("resouce:operation", "operator"); Assert.IsTrue(cache.GetRoles("resouce:operation").Contains("operator")); }