Example #1
0
		static PackedChannelPermissions()
		{
			None = new PackedChannelPermissions();
			None.Lock();
			All = new PackedChannelPermissions(Convert.ToUInt32("00000011111100111111110000011001", 2));
			All.Lock();
        }
Example #2
0
			internal PermissionOverwrite(string type, string targetId, uint allow, uint deny)
			{
				TargetType = type;
				TargetId = targetId;
				Allow = new PackedChannelPermissions(allow);
				Deny = new PackedChannelPermissions( deny);
				Allow.Lock();
				Deny.Lock();
			}
Example #3
0
		public Task SetChannelRolePermissions(string channelId, string userId, PackedChannelPermissions allow = null, PackedChannelPermissions deny = null)
			=> SetChannelPermissions(_channels[channelId], userId, PermissionTarget.Role, allow, deny);
Example #4
0
		private async Task SetChannelPermissions(Channel channel, string targetId, string targetType, PackedChannelPermissions allow = null, PackedChannelPermissions deny = null)
		{
			CheckReady();
			if (channel == null) throw new NullReferenceException(nameof(channel));
			if (targetId == null) throw new NullReferenceException(nameof(targetId));
			if (targetType == null) throw new NullReferenceException(nameof(targetType));

			uint allowValue = allow?.RawValue ?? 0;
			uint denyValue = deny?.RawValue ?? 0;
			bool changed = false;

			var perms = channel.PermissionOverwrites.Where(x => x.TargetType != targetType || x.TargetId != targetId).FirstOrDefault();
			if (allowValue != 0 || denyValue != 0)
			{
				await _api.SetChannelPermissions(channel.Id, targetId, targetType, allowValue, denyValue);
				if (perms != null)
				{
                    perms.Allow.SetRawValueInternal(allowValue);
					perms.Deny.SetRawValueInternal(denyValue);
				}
				else
				{
					var oldPerms = channel._permissionOverwrites;
					var newPerms = new Channel.PermissionOverwrite[oldPerms.Length + 1];
					Array.Copy(oldPerms, newPerms, oldPerms.Length);
					newPerms[oldPerms.Length] = new Channel.PermissionOverwrite(targetType, targetId, allowValue, denyValue);
					channel._permissionOverwrites = newPerms;
				}
				changed = true;
			}
			else
			{
				try
				{
					await _api.DeleteChannelPermissions(channel.Id, targetId);
					if (perms != null)
					{
						channel._permissionOverwrites = channel.PermissionOverwrites.Where(x => x.TargetType != targetType || x.TargetId != targetId).ToArray();
						changed = true;
					}
				}
				catch (HttpException ex) when (ex.StatusCode == HttpStatusCode.NotFound) { }
			}

			if (changed)
			{
				if (targetType == PermissionTarget.Role)
					channel.InvalidatePermissionsCache();
				else if (targetType == PermissionTarget.Member)
					channel.InvalidatePermissionsCache(targetId);
			}
		}
Example #5
0
		public Task SetChannelRolePermissions(Channel channel, string userId, PackedChannelPermissions allow = null, PackedChannelPermissions deny = null)
			=> SetChannelPermissions(channel, userId, PermissionTarget.Role, allow, deny);
Example #6
0
		public Task SetChannelRolePermissions(Channel channel, Role role, PackedChannelPermissions allow = null, PackedChannelPermissions deny = null)
			=> SetChannelPermissions(channel, role?.Id, PermissionTarget.Role, allow, deny);
Example #7
0
		public Task SetChannelUserPermissions(string channelId, User user, PackedChannelPermissions allow = null, PackedChannelPermissions deny = null)
			=> SetChannelPermissions(_channels[channelId], user?.Id, PermissionTarget.Member, allow, deny);
Example #8
0
		public Task SetChannelUserPermissions(Channel channel, User user, PackedChannelPermissions allow = null, PackedChannelPermissions deny = null)
			=> SetChannelPermissions(channel, user?.Id, PermissionTarget.Member, allow, deny);
Example #9
0
		internal void AddChannel(string channelId)
		{
			var perms = new PackedChannelPermissions();
			perms.Lock();
            _permissions.TryAdd(channelId, perms);
			UpdatePermissions(channelId);
        }