packages/sql-semantics-mysql/src/Statement/Account/Privilege/GrantPrivileges.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Statement\Account\Privilege;
6
7use SqlSemantics\Platform\MySql\Model\Construction\Derivation;
8use SqlSemantics\Platform\MySql\Model\Diagnostic\Check;
9use SqlSemantics\Platform\MySql\Model\Rendering\Output;
10use SqlSemantics\Platform\MySql\Model\Statement\Snapshot;
11use SqlSemantics\Platform\MySql\Model\Statement\Statement;
12use SqlSemantics\Platform\MySql\Rules\Account\AccountChecks;
13use SqlSemantics\Platform\MySql\Rules\Account\PrivilegeChecks;
14use SqlSemantics\Platform\MySql\Statement\Account\Option\TlsRequirement;
15use SqlSemantics\Platform\MySql\Statement\Account\Privilege\Item\AllPrivileges;
16use SqlSemantics\Platform\MySql\Statement\Account\Privilege\Item\Grantable;
17use SqlSemantics\Platform\MySql\Statement\Account\Privilege\Item\GrantedRole;
18use SqlSemantics\Platform\MySql\Statement\Account\Privilege\Level\ObjectKind;
19use SqlSemantics\Platform\MySql\Statement\Account\Privilege\Level\PrivilegeLevel;
20use SqlSemantics\Platform\MySql\Statement\Account\User\UserSpecification;
21
22/**
23 * `GRANT privileges ON [kind] level TO accounts [REQUIRE …] [WITH …] [AS user [WITH ROLE …]]`: a request to grant privileges.
24 *
25 * Mirrors the SQLCOM_GRANT path of LEX (grant, columns, type, users_list,
26 * grant_as). Rule: MYSQL-GRANT-001. The level and its table are derived by
27 * MYSQL-PRIVILEGE-CHECKS-001: a table level records the resolution of the
28 * table as the relation fact of the level. MySQL 5.x also writes an
29 * IDENTIFIED clause per account, a REQUIRE clause and resource limits in the
30 * WITH clause, which set the account as CREATE USER and ALTER USER do; their
31 * problems are reported by MYSQL-ACCOUNT-CHECKS-001. The statement provides
32 * no declaration. ON TABLE and ON are the same request; ALL and ALL
33 * PRIVILEGES are the same request.
34 * Source: https://dev.mysql.com/doc/refman/8.4/en/grant.html,
35 * https://dev.mysql.com/doc/refman/5.7/en/grant.html. Status: Implemented.
36 *
37 * @visibility public
38 * @example Granting table privileges with the grant option
39 *     (new \SqlSemantics\Platform\MySql\Semantics())->analyze('grant select, insert on table shop.t to app@localhost with grant option')->toString() // => 'GRANT SELECT, INSERT ON shop.t TO app@localhost WITH GRANT OPTION'
40 */
41final class GrantPrivileges implements Statement
42{
43    use Snapshot;
44
45    /**
46     * @var list<Grantable> The granted privileges in order; ALL stands alone
47     */
48    public readonly array $privileges;
49
50    /**
51     * @var list<UserSpecification> The accounts privileges are granted to
52     */
53    public readonly array $grantees;
54
55    /**
56     * @var list<WithOption> The options of the WITH clause in order
57     */
58    public readonly array $options;
59
60    /**
61     * @param list<Grantable> $privileges The granted privileges in order; ALL stands alone
62     * @param ObjectKind $kind The kind of object the level names
63     * @param PrivilegeLevel $level The privilege level
64     * @param list<UserSpecification> $grantees The accounts privileges are granted to; at least one
65     * @param TlsRequirement|null $tls The REQUIRE clause of MySQL 5.x, when written
66     * @param list<WithOption> $options The options of the WITH clause in order
67     * @param GrantAs|null $as The AS clause, when written
68     */
69    public function __construct(
70        array $privileges,
71        public readonly ObjectKind $kind,
72        public readonly PrivilegeLevel $level,
73        array $grantees,
74        public readonly ?TlsRequirement $tls = null,
75        array $options = [],
76        public readonly ?GrantAs $as = null,
77    ) {
78        $this->privileges = Check::listOf($privileges, Grantable::class, 'GRANT names at least one privilege.', 1);
79        $this->grantees = Check::listOf($grantees, UserSpecification::class, 'GRANT names at least one account.', 1);
80        $this->options = Check::listOf($options, WithOption::class, 'The WITH clause lists options.');
81        foreach ($this->privileges as $privilege) {
82            Check::input(!$privilege instanceof AllPrivileges || count($this->privileges) === 1, 'ALL is the only item of its list.');
83            Check::input(!$privilege instanceof GrantedRole || $privilege->role->host !== null, 'A role without a host in a privilege list reads as a dynamic privilege.');
84        }
85        foreach ($this->grantees as $grantee) {
86            Check::input($grantee->granted(), 'A GRANT account is a named account with at most one authentication method.');
87        }
88    }
89
90    /**
91     * Derives the level, the table and the account clauses.
92     */
93    public function deriveStatement(Derivation $derivation): void
94    {
95        (new PrivilegeChecks())->grant($derivation, $this->privileges, $this->kind, $this->level);
96        (new AccountChecks())->tls($derivation, $this->tls);
97    }
98
99    /**
100     * Writes the request.
101     */
102    public function render(Output $out): void
103    {
104        $out->keyword('GRANT')->list($this->privileges)->keyword('ON');
105        $keyword = $this->kind->keyword();
106        if ($keyword !== null) {
107            $out->keyword($keyword);
108        }
109        $out->node($this->level)->keyword('TO')->list($this->grantees)->node($this->tls);
110        if ($this->options !== []) {
111            $out->keyword('WITH');
112            foreach ($this->options as $option) {
113                $out->node($option);
114            }
115        }
116        $out->node($this->as);
117    }
118}
119