Esempio n. 1
0
        public async Task <IReactionHandler.Result> HandleRemoveAsync(MessageReactionRemoveEventArgs eventArgs)
        {
            if (!(eventArgs.Message.Id == _config.VerificationMessageId &&
                  eventArgs.Message.ChannelId == _config.VerificationChannelId))
            {
                return(IReactionHandler.Result.Continue);
            }
            if (!eventArgs.Emoji.Name.Equals(_config.StaffVerificationEmojiName))
            {
                return(IReactionHandler.Result.Continue);
            }

            bool ungranted = await _roleManager.RevokeRolesPoolAsync(eventArgs.User.Id, RolesPool.Staff);

            if (!ungranted)
            {
                _logger.LogWarning("Ungranting roles for user {0} (id {1}) failed.", eventArgs.User.Username, eventArgs.User.Id);
                DiscordDmChannel channel = await eventArgs.Guild.Members[eventArgs.User.Id].CreateDmChannelAsync();
                await channel.SendMessageAsync("Staff role se nepodařilo odebrat. Prosím, kontaktujte moderátory.");
            }

            return(IReactionHandler.Result.Continue);
        }
        public async Task <IAuthorizationService.AuthorizeResult> AuthorizeAsync(string accessToken, string username, ulong userId, RolesPool rolesPool)
        {
            bool discordIdPresent = await IsUserVerified(userId);

            UsermapPerson?person = await _usermapInfoService.GetUserInfoAsync(accessToken, username);

            if (person == null)
            {
                _logger.LogWarning("Couldn't fetch info from UserMap");
                return(IAuthorizationService.AuthorizeResult.UserMapError);
            }

            string authId      = _hashService.Hash(person.Username);
            bool   authPresent = await _dbContext.Verifications.AnyAsync(v => v.AuthId == authId);

            IReadOnlySet <DiscordRole> discordRoles = _roleManager.MapUsermapRoles(person.Roles, rolesPool);

            // discord and auth -> update roles
            if (discordIdPresent && authPresent)
            {
                bool verificationExists =
                    await _dbContext.Verifications.AnyAsync(v => v.UserId == userId && v.AuthId == authId);

                if (verificationExists)
                {
                    bool ungranted = await _roleManager.RevokeRolesPoolAsync(userId, rolesPool);

                    if (!ungranted)
                    {
                        _logger.LogWarning("Ungranting roles pool {2} for {0} (id {1}) failed.", username, userId, rolesPool);
                        return(IAuthorizationService.AuthorizeResult.Failed);
                    }
                    bool granted = await _roleManager.GrantRolesAsync(userId, discordRoles);

                    return(granted ? IAuthorizationService.AuthorizeResult.OK : IAuthorizationService.AuthorizeResult.Failed);
                }

                return(IAuthorizationService.AuthorizeResult.DifferentMember);
            }

            // discord xor auth -> user already verified, error
            if (discordIdPresent || authPresent)
            {
                return(IAuthorizationService.AuthorizeResult.DifferentMember);
            }

            // nothing -> create database entry, update roles
            {
                bool rolesGranted = await _roleManager.GrantRolesAsync(userId, discordRoles);

                if (rolesGranted)
                {
                    Verification verification = new() { AuthId = authId, UserId = userId };

                    await _dbContext.Verifications.AddAsync(verification);

                    await _dbContext.SaveChangesAsync();

                    await _roleManager.RevokeHostRolesAsync(userId);

                    return(IAuthorizationService.AuthorizeResult.OK);
                }

                return(IAuthorizationService.AuthorizeResult.Failed);
            }
        }