Skip to content
wiki.fftac.org

Permissionrepository

**Site relevance:** Spiralist.org

**Memory type:** plugin source memory

**Source path:** Spiralist/wp-content/plugins/spiralist-workspace/includes/Domain/PermissionRepository.php

**Size:** 3.1 KB

Summary

namespace SpiralistWorkspace\Domain;

Source Preview

This source file is short enough to preview directly on its source-memory page.

<?php

namespace SpiralistWorkspace\Domain;

use SpiralistWorkspace\Installer;

if (!defined('ABSPATH')) {
    exit;
}

/**
 * Stores explicit prompt sharing grants.
 */
class PermissionRepository
{
    private const SUPPORTED_PERMISSION_TYPES = ['view'];

    /**
     * Grants access to a prompt for a specific user.
     */
    public function grant(int $prompt_id, int $shared_with_user_id, int $granted_by_user_id, string $permission_type = 'view'): bool
    {
        global $wpdb;
        $permission_type = $this->normalize_permission_type($permission_type);

        $result = $wpdb->replace(
            Installer::table('prompt_permissions'),
            [
                'prompt_post_id' => $prompt_id,
                'shared_with_user_id' => $shared_with_user_id,
                'granted_by_user_id' => $granted_by_user_id,
                'permission_type' => sanitize_key($permission_type),
                'created_utc' => gmdate('Y-m-d H:i:s'),
            ],
            ['%d', '%d', '%d', '%s', '%s']
        );

        return (bool) $result;
    }

    /**
     * Revokes access for a specific user.
     */
    public function revoke(int $prompt_id, int $shared_with_user_id, string $permission_type = 'view'): bool
    {
        global $wpdb;
        $permission_type = $this->normalize_permission_type($permission_type);

        $deleted = $wpdb->delete(
            Installer::table('prompt_permissions'),
            [
                'prompt_post_id' => $prompt_id,
                'shared_with_user_id' => $shared_with_user_id,
                'permission_type' => sanitize_key($permission_type),
            ],
            ['%d', '%d', '%s']
        );

        return (bool) $deleted;
    }

    /**
     * Returns whether the user has explicit access to the prompt.
     */
    public function user_has_access(int $prompt_id, int $user_id): bool
    {
        global $wpdb;

        $sql = $wpdb->prepare(
            'SELECT COUNT(*) FROM ' . Installer::table('prompt_permissions') . ' WHERE prompt_post_id = %d AND shared_with_user_id = %d AND permission_type = %s',
            $prompt_id,
            $user_id,
            'view'
        );

        return (int) $wpdb->get_var($sql) > 0;
    }

    /**
     * Lists share grants for a prompt.
     */
    public function list_for_prompt(int $prompt_id): array
    {
        global $wpdb;

        $sql = $wpdb->prepare(
            'SELECT * FROM ' . Installer::table('prompt_permissions') . ' WHERE prompt_post_id = %d ORDER BY created_utc DESC',
            $prompt_id
        );

        $rows = $wpdb->get_results($sql, ARRAY_A);

        return is_array($rows) ? $rows : [];
    }

    /**
     * Normalizes permission types to the supported set.
     */
    private function normalize_permission_type(string $permission_type): string
    {
        $permission_type = sanitize_key($permission_type);

        if (!in_array($permission_type, self::SUPPORTED_PERMISSION_TYPES, true)) {
            return 'view';
        }

        return $permission_type;
    }
}