/// <summary> /// Remove cached record /// </summary> public void Clear(string roleId, ResourcePermissionKey permissionUid) { var roleLists = _roleCollection.Values.Where(r => r.Ids.Contains(roleId)); foreach (var roleList in roleLists) { roleList.Permissions.RemoveAll(p => p.Uid == permissionUid); } }
public bool?IsAllowed(ICollection <string> roleIds, ResourcePermissionKey permissionUid, string instanceId) { using (var db = _databaseFactory.Create()) { var privilege = db.Permission.FirstOrDefault(p => roleIds.Contains(p.RoleId) && p.ResourceId == permissionUid.ResourceId && p.PermissionId == permissionUid.PermissionId && p.InstanceId == instanceId); return(privilege?.IsAllowed); } }
public void Load_CallOnce_LoadDataFromStore() { var sut = new PermissionCache(); var count = 0; var roles = new List <string> { "Role1", "Role2" }; var permission = new ResourcePermissionKey { PermissionId = 1, ResourceId = 2 }; var instance = "0"; sut.Load(() => { count++; return(true); }, roles, permission, instance); Assert.AreEqual(1, count); }
public void Load_CallTwiceWithTheSameDataInTheSameRoleOrder_OnlyOnceCallsStore() { var sut = new PermissionCache(); var count = 0; var roles = new List <string> { "Role1", "Role2" }; var permission = new ResourcePermissionKey { PermissionId = 1, ResourceId = 2 }; var instance = "0"; sut.Load(() => { count++; return(true); }, roles, permission, instance); sut.Load(() => { count++; return(true); }, roles, permission, instance); Assert.AreEqual(1, count); }
public void Clear_CallTwiceWithTheSameDataInDifferentRoleOrder_OnlyOnceCallsStore(string clearedRole, int expectedCalls) { var sut = new PermissionCache(); var count = 0; var roles = new List <string> { "Role1", "Role2" }; var permission = new ResourcePermissionKey { PermissionId = 1, ResourceId = 2 }; var instance = "0"; sut.Load(() => { count++; return(true); }, roles, permission, instance); sut.Clear(clearedRole, permission); sut.Load(() => { count++; return(true); }, roles, permission, instance); Assert.AreEqual(expectedCalls, count); }
public bool?Load(Func <bool?> callback, ICollection <string> roleIds, ResourcePermissionKey permissionUid, string instanceId) { var sortedIds = roleIds.OrderBy(i => i).ToList(); var key = string.Join("|#|", sortedIds); //Find or create roles if (!_roleCollection.TryGetValue(key, out var roleList)) { _roleCollection[key] = roleList = new RoleList(sortedIds); } //Find or create permission var existingPermission = roleList.Permissions.FirstOrDefault(p => p.Uid == permissionUid && p.InstanceId == instanceId); if (existingPermission == null) { var isAllowed = callback(); existingPermission = new Permission(permissionUid, instanceId, isAllowed); roleList.Permissions.Add(existingPermission); } return(existingPermission.IsAllowed); }
public bool?IsAllowed(string roleId, ResourcePermissionKey permissionUid, string instanceId) { return(IsAllowed(new[] { roleId }, permissionUid, instanceId)); }
public Permission(ResourcePermissionKey uid, string instanceId, bool?isAllowed) { Uid = uid; InstanceId = instanceId; IsAllowed = isAllowed; }