aboutsummaryrefslogtreecommitdiff
path: root/app/class/Modeluser.php
blob: 256ad55c2f7afbeeeeef24abc11812d217902cf2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php

namespace Wcms;

use JamesMoss\Flywheel\Document;

class Modeluser extends Modeldb
{
    public const ADMIN = 10;
    public const SUPEREDITOR = 4;
    public const EDITOR = 3;
    public const INVITE = 2;
    public const READ = 1;
    public const FREE = 0;

    public const USER_REPO_NAME = 'user';

    public function __construct()
    {
        parent::__construct();
        $this->storeinit(self::USER_REPO_NAME);
    }

    /**
     * Write session cookie according to users datas and define the current authtoken being used
     *
     * @param User $user Current user to keep in session
     */
    public function writesession(User $user)
    {
        $_SESSION['user' . Config::basepath()]['level'] = $user->level();
        $_SESSION['user' . Config::basepath()]['id'] = $user->id();
        $_SESSION['user' . Config::basepath()]['columns'] = $user->columns();
    }


    public function readcookie()
    {
        if (isset($_COOKIE['authtoken']) && strpos($_COOKIE['authtoken'], ':')) {
            list($cookietoken, $cookiemac) = explode(':', $_COOKIE['authtoken']);
            $authtokenmanager = new Modelauthtoken();
            $dbtoken = $authtokenmanager->getbytoken($cookietoken);

            if ($dbtoken !== false) {
                if (hash_equals($cookiemac, secrethash($dbtoken->getId()))) {
                    $user = $this->get($dbtoken->user);
                    return $user;
                }
            }
        }
        return false;
    }


    public function logout()
    {
        $user = new User(['level' => self::FREE]);
        return $user;
    }



    /**
     * @return User[] associative array of User objects `id => User`
     */
    public function getlister()
    {
        $userlist = [];
        $list = $this->repo->findAll();
        foreach ($list as $userdata) {
            $userlist[$userdata->id] = new User($userdata);
        }
        return $userlist;
    }


    public function pagelistbyid(array $idlist = [])
    {
        $userdatalist = $this->repo->query()
            ->where('__id', 'IN', $idlist)
            ->execute();

        $userlist = [];
        foreach ($userdatalist as $id => $userdata) {
            $userlist[$id] = new User($userdata);
        }
        return $userlist;
    }

    public function admincount()
    {
        $userdatalist = $this->repo->query()
            ->where('level', '==', 10)
            ->execute();

        return $userdatalist->total();
    }

    public function getlisterbylevel(int $level, $comp = '==')
    {
        $userdatalist = $this->repo->query()
            ->where('level', $comp, $level)
            ->execute();

        $userlist = [];
        foreach ($userdatalist as $user) {
            $userlist[] = $user->id;
        }

        return $userlist;
    }

    /**
     * Check if the password is used, and return by who
     *
     * @param string $userid user ID
     * @param string $pass password clear
     *
     * @return User|bool User or false
     */
    public function passwordcheck(string $userid, string $pass)
    {
        $user = $this->get($userid);
        if ($user !== false) {
            if ($user->passwordhashed()) {
                if (password_verify($pass, $user->password())) {
                    return $user;
                }
            } else {
                if ($user->password() === $pass) {
                    return $user;
                }
            }
        }
        return false;
    }

    /**
     * @param User $user
     *
     * @return bool depending on success
     */
    public function add(User $user): bool
    {
        $userdata = new Document($user->dry());
        $userdata->setId($user->id());
        return $this->repo->store($userdata);
    }


    /**
     * @param string|User $id Can be an User object or a string ID
     *
     * @return User|false User object or false in case of error
     */
    public function get($id)
    {
        if ($id instanceof User) {
            $id = $id->id();
        }
        if (is_string($id)) {
            $userdata = $this->repo->findById($id);
            if ($userdata !== false) {
                return new User($userdata);
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public function delete(User $user)
    {
        $this->repo->delete($user->id());
    }
}