public async Task <PermissionState> GetPermissionStateAsync(Permission permission, ulong channel_id) { using (ValourDB Context = new ValourDB(ValourDB.DBOptions)) { ChatChannelPermissionsNode node = await Context.ChatChannelPermissionsNodes.FirstOrDefaultAsync(x => x.Role_Id == Id && x.Channel_Id == channel_id); return(node.GetPermissionState(permission)); } }
/// <summary> /// Returns if a given member has a channel permission /// </summary> public async Task <bool> HasPermission(ServerPlanetMember member, ChatChannelPermission permission, ValourDB db) { Planet ??= await GetPlanetAsync(db); if (Planet.Owner_Id == member.User_Id) { return(true); } // If true, we just ask the category if (Inherits_Perms) { if (Parent == null) { Parent = await GetParentAsync(db); } return(await Parent.HasPermission(member, permission)); } // Load permission data await db.Entry(member).Collection(x => x.RoleMembership) .Query() .Where(x => x.Planet_Id == Planet.Id) .Include(x => x.Role) .ThenInclude(x => x.ChatChannelPermissionNodes.Where(x => x.Channel_Id == Id)) .LoadAsync(); // Starting from the most important role, we stop once we hit the first clear "TRUE/FALSE". // If we get an undecided, we continue to the next role down foreach (var roleMembership in member.RoleMembership) { var role = roleMembership.Role; ChatChannelPermissionsNode node = role.ChatChannelPermissionNodes.FirstOrDefault(); // If we are dealing with the default role and the behavior is undefined, we fall back to the default permissions if (node == null) { if (role.Id == Planet.Default_Role_Id) { return(Permission.HasPermission(ChatChannelPermissions.Default, permission)); } continue; } PermissionState state = node.GetPermissionState(permission); if (state == PermissionState.Undefined) { continue; } else if (state == PermissionState.True) { return(true); } else { return(false); } } // No roles ever defined behavior: resort to false. return(false); }