コード例 #1
0
        /// <summary>
        /// Tests that flag of the given permissions value to be the expected output
        /// and then tries cycling through the states of the allow and deny values
        /// for that flag
        /// </summary>
        /// <param name="value"></param>
        /// <param name="flag"></param>
        /// <param name="expected"></param>
        private void TestHelper(OverwritePermissions value, ChannelPermission flag, PermValue expected)
        {
            // check that the value matches
            Assert.Equal(expected, Permissions.GetValue(value.AllowValue, value.DenyValue, flag));

            // check toggling bits for both allow and deny
            // have to make copies to get around read only property
            ulong allow = value.AllowValue;
            ulong deny  = value.DenyValue;

            // both unset should be inherit
            Permissions.UnsetFlag(ref allow, (ulong)flag);
            Permissions.UnsetFlag(ref deny, (ulong)flag);
            Assert.Equal(PermValue.Inherit, Permissions.GetValue(allow, deny, flag));

            // allow set should be allow
            Permissions.SetFlag(ref allow, (ulong)flag);
            Permissions.UnsetFlag(ref deny, (ulong)flag);
            Assert.Equal(PermValue.Allow, Permissions.GetValue(allow, deny, flag));

            // deny should be deny
            Permissions.UnsetFlag(ref allow, (ulong)flag);
            Permissions.SetFlag(ref deny, (ulong)flag);
            Assert.Equal(PermValue.Deny, Permissions.GetValue(allow, deny, flag));

            // allow takes precedence
            Permissions.SetFlag(ref allow, (ulong)flag);
            Permissions.SetFlag(ref deny, (ulong)flag);
            Assert.Equal(PermValue.Allow, Permissions.GetValue(allow, deny, flag));
        }
コード例 #2
0
        public async Task <bool> HasPermission(ServerPlanetMember member, ChannelPermission permission)
        {
            var roles = await member.GetRolesAsync();

            // 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).GetChannelNodeAsync(this);

                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);
        }
コード例 #3
0
        public async Task <int?> Create(Channel model, string userId)
        {
            using (IDbConnection connection = new SqlConnection(_dbSettings.DefaultConnection))
            {
                int?id = await connection.InsertAsync(model);

                var channelTags = model.Tags
                                  .Select(tag => new ChannelTag {
                    ChannelId = model.Id, TagId = tag.Id
                });

                var channelPermission = new ChannelPermission
                {
                    ChannelId   = id.Value,
                    UserId      = userId,
                    ChannelRole = ChannelRole.Owner
                };
                const string insertPermissionsSql = "INSERT INTO [ChannelPermissions] (ChannelId, UserId, ChannelRole) VALUES (@ChannelId, @UserId, @ChannelRole)";
                await connection.ExecuteAsync(insertPermissionsSql, channelPermission);

                const string insertChannelTagSql = "INSERT INTO [ChannelTags] (ChannelId, TagId) VALUES (@ChannelId, @TagId)";
                await Task.WhenAll(channelTags.Select(ct
                                                      => connection.ExecuteAsync(insertChannelTagSql, ct)));

                return(id);
            }
        }
コード例 #4
0
        public async Task <IActionResult> PutChannelPermission(int id, ChannelPermission channelPermission)
        {
            if (id != channelPermission.ChannelId)
            {
                return(BadRequest());
            }

            _context.Entry(channelPermission).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ChannelPermissionExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
コード例 #5
0
        /// <summary>Whether the bot has the permission to perform an action given the command context.</summary>
        public static bool BotCan(this SocketCommandContext context, ChannelPermission permission)
        {
            var perms = context.Guild != null
                ? (ChannelPermission)context.Guild.CurrentUser.GetPermissions(context.Channel as IGuildChannel).RawValue
                : CorrectDmPermissions;

            return(perms.HasFlag(permission));
        }
コード例 #6
0
        // Permissions


        /// <summary>Whether the bot has the permission to perform an action in the given chanel.</summary>
        public static bool BotCan(this IChannel channel, ChannelPermission permission)
        {
            var perms = channel is IGuildChannel gchannel
                ? (ChannelPermission)gchannel.Guild.GetCurrentUserAsync().Result.GetPermissions(gchannel).RawValue
                : CorrectDmPermissions;

            return(perms.HasFlag(permission));
        }
コード例 #7
0
 public Context CheckAuthorPermission(ChannelPermission permission, string permissionName)
 {
     if (!GetChannelPermissions(Author).Has(permission))
     {
         throw new PKError($"You must have the \"{permissionName}\" permission in this server to use this command.");
     }
     return(this);
 }
コード例 #8
0
ファイル: GuildHandler.cs プロジェクト: Lomztein/Moduthulhu
        public void AssertChannelPermission(ChannelPermission permission, ulong channelId)
        {
            IGuildChannel channel = GetChannel(channelId);

            if (!HasChannelPermission(permission, channelId))
            {
                throw new MissingPermissionException($"Bot does not have channel {nameof (permission)} '{permission} in channel '{channel.Name}'");
            }
        }
コード例 #9
0
        /// <summary>
        /// Casts the <see cref="ChannelPermissions"/> struct to the <see cref="ChannelPermission"/> enum.
        /// </summary>
        /// <param name="perms">The <see cref="ChannelPermissions"/> struct.</param>
        /// <returns>The <see cref="ChannelPermission"/> enum</returns>
        public static ChannelPermission ToEnum(this ChannelPermissions perms)
        {
            ChannelPermission outPerms = 0;

            foreach (ChannelPermission perm in perms.ToList())
            {
                outPerms |= perm;
            }
            return(outPerms);
        }
コード例 #10
0
ファイル: Extentions.cs プロジェクト: LulzYT/NoAdsHere
        public static bool CheckChannelPermission(this IMessageChannel channel, ChannelPermission permission, IGuildUser guildUser)
        {
            var guildchannel = channel as IGuildChannel;

            ChannelPermissions perms;

            perms = guildchannel != null?guildUser.GetPermissions(guildchannel) : ChannelPermissions.All(null);

            return(perms.Has(permission));
        }
コード例 #11
0
        public async Task <bool> SetChannelPermissionAsync(ulong guildId, ulong channelId, string permissionName, bool?granted)
        {
            using var scope = _provider.CreateScope();
            var repo       = scope.ServiceProvider.GetRequiredService <IChannelPermissionRepository>();
            var permission = await repo.GetAsync(guildId, channelId, permissionName);

            if (permission == null)
            {
                if (!granted.HasValue)
                {
                    return(true);
                }

                permission = new ChannelPermission
                {
                    GuildId   = guildId,
                    ChannelId = channelId,
                    Name      = permissionName,
                    Granted   = granted.Value
                };

                repo.Add(permission);
                await repo.SaveChangesAsync();
            }
            else if (!granted.HasValue || permission.Granted != granted)
            {
                if (granted.HasValue)
                {
                    permission.Granted = granted.Value;

                    repo.Update(permission);
                }
                else
                {
                    repo.Remove(permission);
                }

                await repo.SaveChangesAsync();
            }
            else
            {
                return(false);
            }

            await _cache.RemoveAsync(CacheKey.GetChannelPermissionCacheKey(guildId, channelId));

            return(true);
        }
コード例 #12
0
        private static string GetPermissionDisplayName(ChannelPermission permission)
        {
            switch (permission)
            {
            case ChannelPermission.DeafenMembers:
                return("Deafen Members");

            case ChannelPermission.MoveMembers:
                return("Move Members");

            case ChannelPermission.MuteMembers:
                return("Mute Members");

            default:
                return(permission.ToString());
            }
        }
コード例 #13
0
        public async Task <ActionResult <ChannelPermission> > PostChannelPermission(ChannelPermission channelPermission)
        {
            _context.ChannelPermission.Add(channelPermission);
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (ChannelPermissionExists(channelPermission.ChannelId))
                {
                    return(Conflict());
                }
                else
                {
                    throw;
                }
            }

            return(CreatedAtAction("GetChannelPermission", new { id = channelPermission.ChannelId }, channelPermission));
        }
コード例 #14
0
        /// <summary>
        /// <see cref="RequireUserPermissionAttribute"/>, but it isn't an attribute
        /// </summary>
        /// <param name="context">Context</param>
        /// <param name="permission">The permission the user needs</param>
        /// <returns>True if the user has permissions, else false. Also returns false if the command wasn't executed in a guild.</returns>
        public static bool CheckForPermissions(ICommandContext context, ChannelPermission permission)
        {
            var guildUser = context.User as IGuildUser;
            ChannelPermissions perms;

            if (context.Channel is IGuildChannel guildChannel)
            {
                perms = guildUser.GetPermissions(guildChannel);
            }
            else
            {
                perms = ChannelPermissions.All(context.Channel);
            }

            if (perms.Has(permission))
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
コード例 #15
0
 public static void SetValue(ref ulong rawValue, bool?value, ChannelPermission bit)
 => SetValue(ref rawValue, value, (byte)bit);
コード例 #16
0
 public static bool GetValue(ulong value, ChannelPermission bit)
 => GetValue(value, (byte)bit);
コード例 #17
0
ファイル: DiscordUtils.cs プロジェクト: cloutmuppet/PluralKit
 public static async Task <bool> HasPermission(this IChannel channel, ChannelPermission permission) =>
 (await PermissionsIn(channel)).Has(permission);
コード例 #18
0
ファイル: Permissions.cs プロジェクト: breadbyte/Discord.Net
 public static PermValue GetValue(ulong allow, ulong deny, ChannelPermission flag)
 => GetValue(allow, deny, (ulong)flag);
コード例 #19
0
ファイル: Permissions.cs プロジェクト: breadbyte/Discord.Net
 public static void SetValue(ref ulong allow, ref ulong deny, PermValue?value, ChannelPermission flag)
 => SetValue(ref allow, ref deny, value, (ulong)flag);
コード例 #20
0
ファイル: Permissions.cs プロジェクト: breadbyte/Discord.Net
 public static void SetValue(ref ulong rawValue, bool?value, ChannelPermission flag)
 => SetValue(ref rawValue, value, (ulong)flag);
コード例 #21
0
ファイル: Permissions.cs プロジェクト: breadbyte/Discord.Net
 public static bool GetValue(ulong value, ChannelPermission flag)
 => GetValue(value, (ulong)flag);
コード例 #22
0
 public UserPermission(ChannelPermission channelPermission, string message)
 {
     ErrorMessage      = message;
     ChannelPermission = channelPermission;
     Permission        = PermissionType.Channel;
 }
コード例 #23
0
 public static bool HasPermission(this SocketTextChannel channel, SocketGuildUser user, ChannelPermission permission)
 {
     return(user.GetPermissions(channel).ToList().Contains(permission));
 }
コード例 #24
0
 public static bool HasPermission(this SocketTextChannel context, ChannelPermission permission)
 {
     return(context.Guild.CurrentUser.GetPermissions(context).ToList().Contains(permission));
 }
コード例 #25
0
 public BotPermissionsAttribute(ChannelPermission channelPermission, string failureString = null)
 {
     this.ChannelPermission = channelPermission;
     this.FailureString     = failureString;
 }
コード例 #26
0
 /// <summary>
 /// Check if the bot has permission <paramref name="perm"/> in this plugins assigned server in a channel with id <paramref name="channelId"/>.
 /// </summary>
 /// <param name="perm">The permission to check</param>
 /// <returns>True if bot has permission, otherwise false.</returns>
 protected void HasChannelPermission(ChannelPermission perm, ulong channelId) => GuildHandler.HasChannelPermission(perm, channelId);
コード例 #27
0
 public static void SetValue(ref ulong allow, ref ulong deny, PermValue?value, ChannelPermission bit)
 => SetValue(ref allow, ref deny, value, (byte)bit);
コード例 #28
0
 public static bool BotHasPermission(this Context ctx, ChannelPermission permission) => BotPermissions(ctx).Has(permission);
コード例 #29
0
 public static PermValue GetValue(ulong allow, ulong deny, ChannelPermission bit)
 => GetValue(allow, deny, (byte)bit);
コード例 #30
0
 /// <summary>
 /// Require that the user invoking the command has a specified ChannelPermission.
 /// </summary>
 /// <param name="permission">The ChannelPermission that the user must have. Multiple permissions can be specified by ORing the permissions together.</param>
 /// <example>
 /// <code language="c#">
 ///     [Command("permission")]
 ///     [RequireUserPermission(ChannelPermission.ReadMessageHistory | ChannelPermission.ReadMessages)]
 ///     public async Task HasPermission()
 ///     {
 ///         await ReplyAsync("You can read messages and the message history!");
 ///     }
 /// </code>
 /// </example>
 public RequireUserPermissionAttribute(ChannelPermission permission)
 {
     ChannelPermission = permission;
     GuildPermission   = null;
 }