/// <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);
        }
Exemple #2
0
        public async Task <bool> HasPermission(ServerPlanetMember member, Permission permission, ValourDB db = null)
        {
            Planet planet = await GetPlanetAsync(db);

            if (planet.Owner_Id == member.User_Id)
            {
                return(true);
            }

            var roles = await member.GetRolesAsync(db);

            CategoryPermission    catPerm  = null;
            ChatChannelPermission chatPerm = null;

            catPerm  = permission as CategoryPermission;
            chatPerm = permission as ChatChannelPermission;

            // 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 role in roles)
            {
                var node = await ServerPlanetRole.FromBase(role).GetCategoryNodeAsync(this);

                // 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)
                    {
                        if (catPerm != null)
                        {
                            return(Permission.HasPermission(CategoryPermissions.Default, permission));
                        }
                        else if (chatPerm != null)
                        {
                            return(Permission.HasPermission(ChatChannelPermissions.Default, permission));
                        }
                    }

                    continue;
                }

                PermissionState state = PermissionState.Undefined;

                if (catPerm != null)
                {
                    state = node.GetPermissionState(permission);
                }
                else if (chatPerm != null)
                {
                    state = node.GetChatChannelPermissionState(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);
        }