xref: /plugin/structpublish/helper/db.php (revision 910e7e15ab7eacafbaa096c48916ba3bb1c5258f)
1<?php
2
3use dokuwiki\plugin\structpublish\meta\Assignments;
4
5class helper_plugin_structpublish_db extends helper_plugin_struct_db
6{
7    /**
8     * Get list of all pages known to the plugin
9     * @return array
10     */
11    public function getPages($pid = null)
12    {
13        $sql = 'SELECT pid FROM data_structpublish';
14        if ($pid) {
15            $sql .= ' WHERE pid = ?';
16        }
17        $res = $this->sqlite->query($sql, $pid);
18        $list = $this->sqlite->res2arr($res);
19        $this->sqlite->res_close($res);
20        return $list;
21    }
22
23    /**
24     * Overwrites dummy IS_PUBLISHER from struct plugin
25     * Required argument: pid
26     * Expected arguments: user, grps; default to current user and their groups
27     *
28     * Returns true if user/group matches the 'publish' rule for the given page
29     *
30     * @return bool
31     */
32    public function IS_PUBLISHER() // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
33    {
34        global $USERINFO;
35        global $INPUT;
36
37        $args = func_get_args();
38        $pid = $args[0];
39        $userId = $args[1] ?? $INPUT->server->str('REMOTE_USER');
40        $grps = $args[2] ?? ($USERINFO['grps'] ?? []);
41
42        $role = helper_plugin_structpublish_permissions::ACTION_PUBLISH;
43
44        return $this->isRoleAllowed($role, $pid, $userId, $grps);
45    }
46
47    /**
48     * Check for approver role
49     * @see \helper_plugin_structpublish_db::IS_PUBLISHER
50     * @FIXME This is method is not used yet.
51     *
52     * Required argument: pid
53     * Expected arguments: user, grps; default to current user and their groups
54     *
55     * Returns true if user/group matches the 'approve' rule for the given page
56     *
57     * @return bool
58     */
59    public function IS_APPROVER() // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
60    {
61        global $USERINFO;
62        global $INPUT;
63
64        $args = func_get_args();
65        $pid = $args[0];
66        $userId = $args[1] ?? $INPUT->server->str('REMOTE_USER');
67        $grps = $args[2] ?? ($USERINFO['grps'] ?? []);
68
69        $role = helper_plugin_structpublish_permissions::ACTION_APPROVE;
70
71        return $this->isRoleAllowed($role, $pid, $userId, $grps);
72    }
73
74    protected function isRoleAllowed($role, $pid, $userId, $grps)
75    {
76        $assignments = Assignments::getInstance();
77        $rules = $assignments->getPageAssignments($pid);
78
79        if (isset($rules[$role])) {
80            $users = $rules[helper_plugin_structpublish_permissions::ACTION_PUBLISH];
81            if (auth_isMember(implode(',', $users), $userId, $grps)) {
82                return true;
83            }
84        }
85
86        return false;
87    }
88}
89