Exemplo n.º 1
0
        public async Task <ModifyEntityResult> GrantPermissionAsync
        (
            [NotNull] IGuild discordServer,
            [NotNull] IUser discordUser,
            [NotNull] IPermission grantedPermission,
            PermissionTarget target
        )
        {
            // Special All target handling
            if (target == PermissionTarget.All)
            {
                var grantSelfResult = await GrantPermissionAsync
                                      (
                    discordServer,
                    discordUser,
                    grantedPermission,
                    PermissionTarget.Self
                                      );

                var grantOtherResult = await GrantPermissionAsync
                                       (
                    discordServer,
                    discordUser,
                    grantedPermission,
                    PermissionTarget.Other
                                       );

                if (grantSelfResult.IsSuccess || grantOtherResult.IsSuccess)
                {
                    return(ModifyEntityResult.FromSuccess());
                }

                // Both are false, so we'll just inherit the error from the self grant.
                return(ModifyEntityResult.FromError(grantSelfResult));
            }

            var getPermissionResult = await GetOrCreateUserPermissionAsync
                                      (
                discordServer,
                discordUser,
                grantedPermission,
                target
                                      );

            if (!getPermissionResult.IsSuccess)
            {
                return(ModifyEntityResult.FromError(getPermissionResult));
            }

            var permission = getPermissionResult.Entity;

            if (permission.IsGranted)
            {
                return(ModifyEntityResult.FromError("The user already has permission to do that."));
            }

            permission.IsGranted = true;

            await _database.SaveChangesAsync();

            return(ModifyEntityResult.FromSuccess());
        }
Exemplo n.º 2
0
    /// <summary>
    /// Grants the specified user the given permission.
    /// </summary>
    /// <param name="discordServer">The Discord server the permission was granted on.</param>
    /// <param name="discordUser">The Discord user.</param>
    /// <param name="grantedPermission">The granted permission.</param>
    /// <param name="target">The granted target.</param>
    /// <param name="ct">The cancellation token in use.</param>
    /// <returns>A modification result which may or may not have succeeded.</returns>
    public async Task <Result> GrantPermissionAsync
    (
        Snowflake discordServer,
        Snowflake discordUser,
        IPermission grantedPermission,
        PermissionTarget target,
        CancellationToken ct = default
    )
    {
        // Special All target handling
        if (target == PermissionTarget.All)
        {
            var grantSelfResult = await GrantPermissionAsync
                                  (
                discordServer,
                discordUser,
                grantedPermission,
                PermissionTarget.Self,
                ct
                                  );

            var grantOtherResult = await GrantPermissionAsync
                                   (
                discordServer,
                discordUser,
                grantedPermission,
                PermissionTarget.Other,
                ct
                                   );

            if (grantSelfResult.IsSuccess || grantOtherResult.IsSuccess)
            {
                return(Result.FromSuccess());
            }

            // Both are false, so we'll just inherit the error from the self grant.
            return(grantSelfResult);
        }

        var getPermissionResult = await GetOrCreateUserPermissionAsync
                                  (
            discordServer,
            discordUser,
            grantedPermission,
            target,
            ct
                                  );

        if (!getPermissionResult.IsSuccess)
        {
            return(Result.FromError(getPermissionResult));
        }

        var permission = getPermissionResult.Entity;

        if (permission.IsGranted)
        {
            return(new UserError("The user already has permission to do that."));
        }

        permission.IsGranted = true;
        await _database.SaveChangesAsync(ct);

        return(Result.FromSuccess());
    }