1<?php
2
3use dokuwiki\Extension\AuthPlugin;
4
5/**
6 * Class auth_plugin_authgooglesheets
7 */
8class auth_plugin_authgooglesheets extends AuthPlugin
9{
10    /** @var helper_plugin_authgooglesheets */
11    protected $helper;
12
13    /**
14     * Constructor.
15     */
16    public function __construct()
17    {
18        parent::__construct();
19        $this->helper = plugin_load('helper', 'authgooglesheets');
20        if (!$this->helper->validateSheet()) {
21            throw new Exception('User Sheet invalid!');
22        }
23
24        $this->cando['getUsers'] = true;
25        $this->cando['getUserCount'] = true;
26        $this->cando['addUser'] = true;
27        $this->cando['delUser'] = true;
28        $this->cando['modLogin'] = true;
29        $this->cando['modPass'] = true;
30        $this->cando['modName'] = true;
31        $this->cando['modMail'] = true;
32        $this->cando['modGroups'] = true;
33    }
34
35    /**
36     * @inheritDoc
37     */
38    public function checkPass($user, $pass)
39    {
40        $userinfo = $this->getUserData($user);
41        if ($userinfo === false) return false;
42
43        $verified = auth_verifyPassword($pass, $userinfo['pass']);
44
45        // make sure to log the login
46        if ($verified) {
47            $this->helper->update($user, ['lastlogin' => dformat()]);
48        }
49
50        return $verified;
51    }
52
53    /**
54     * Returns user info
55     *
56     * @param string $user
57     * @param bool $requireGroups
58     * @return array|false
59     */
60    public function getUserData($user, $requireGroups = true)
61    {
62        return $this->helper->getUserData($user);
63    }
64
65    /**
66     * Creates a new user
67     *
68     * @param string $user
69     * @param string $pwd
70     * @param string $name
71     * @param string $mail
72     * @param array|null $grps
73     * @return bool|null
74     */
75    public function createUser($user, $pwd, $name, $mail, $grps = null)
76    {
77        global $conf;
78
79        // user mustn't already exist
80        if ($this->getUserData($user) !== false) {
81            msg($this->getLang('userexists'), -1);
82            return false;
83        }
84
85        // the order is important
86        $userData['user'] = $user;
87        $userData['pass'] = $pwd;
88        $userData['name'] = $name;
89        $userData['mail'] = $mail;
90        $userData['created'] = dformat();
91
92        // set default group if no groups specified
93        if (!is_array($grps)) $grps = array($conf['defaultgroup']);
94        $userData['grps'] = implode(',', $grps);
95
96        return $this->helper->appendUser($userData);
97    }
98
99    public function modifyUser($user, $changes)
100    {
101        return $this->helper->update($user, $changes);
102    }
103
104    public function deleteUsers($users)
105    {
106        return $this->helper->delete($users);
107    }
108
109    /**
110     * Info for all users
111     *
112     * @param $start
113     * @param $limit
114     * @param $filter
115     * @return array
116     */
117    public function retrieveUsers($start = 0, $limit = 0, $filter = null)
118    {
119        return $this->helper->getUsers($start, $limit, $filter);
120    }
121
122    /**
123     * Return the number of users which meet $filter criteria
124     *
125     * @param array $filter
126     * @return int
127     */
128    public function getUserCount($filter = array())
129    {
130        return count($this->helper->getUsers(0, 0, $filter));
131    }
132}
133