/// <summary> /// Returns if the given user is authorized to access this planet /// </summary> public async Task <bool> HasPermissionAsync(ServerPlanetMember member, PlanetPermission permission, ValourDB db) { // Special case for viewing planets if (permission.Value == PlanetPermissions.View.Value) { if (Public || (member != null)) { return(true); } } // At this point all permissions require membership if (member == null) { return(false); } // Owner has all permissions if (member.User_Id == Owner_Id) { return(true); } // Get user main role var mainRole = await db.Entry(member).Collection(x => x.RoleMembership) .Query() .Include(x => x.Role) .OrderBy(x => x.Role.Position) .Select(x => x.Role) .FirstAsync(); // Return permission state return(mainRole.HasPermission(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); }