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