aboutsummaryrefslogtreecommitdiff
path: root/models/user.php
blob: 71cf07d1086149720e11ad8b11561f73922df3b9 (plain) (blame)
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
<?php
namespace Database;
use PDO;

class User extends Table {
    public $UID;
    public $Username;
    public $Password;
    public $Role;

    static function create(string $Username, string $Password, string $Role) : int {
        $Password = password_hash($Password, PASSWORD_BCRYPT);
        return Table::_create(
            "Users",
            "(Username, Password, Role)",
            "(\"$Username\", \"$Password\", \"$Role\")",
        );
    }

    static function fromDB(string $username) : User {
        return Table::_fromDB(
            "SELECT * FROM Users WHERE Username = \"$username\"",
            'Database\User'
        );
    }

    static function fromDBuid(int $uid) : User {
        return Table::_fromDB(
            "SELECT * FROM Users WHERE UID = \"$uid\"",
            'Database\User'
        );
    }

    function archives() : array {
        return Table::_get_all(
            'Webpages',
            'Database\Webpage',
            "WHERE RequesterUID = \"$this->UID\" ORDER BY Date DESC"
        );
    }

    function archiveLists() : array {
        return Table::_get_all(
            'ArchiveLists',
            'Database\ArchiveList',
            "WHERE AuthorUID = \"$this->UID\""
        );
    }

    function icon() {
        global $VIEWS_DIR;
        // https://tabler.io/icons
        if ($this->Role === 'User') {
            include $VIEWS_DIR . '/img/user.svg';
        }
        else {
            include $VIEWS_DIR . '/img/user-star.svg';
        }
    }

    private static $AnonUID = 1;

    function update(string $Username, string $Password = null) {
        // Applicable to Anon user
        if ($this->Password === '') {
            throw new Exception('Not modifying system account!');
        }

        $Password = ($Password === null) ? $this->Password : password_hash($Password, PASSWORD_BCRYPT);
        Table::_update(
            'Users',
            "Username = \"$Username\", Password = \"$Password\"",
            "UID = \"$this->UID\""
        );
    }

    function delete() {
        // Applicable to Anon user
        if ($this->Password === '') {
            throw new Exception('Not deleting system account!');
        }

        Table::_update(
            'Webpages',
            'RequesterUID = "' . self::$AnonUID . '"',
            "RequesterUID = \"$this->UID\""
        );

        Table::_update(
            'ArchiveLists',
            'AuthorUID = "' . self::$AnonUID . '"',
            "AuthorUID = \"$this->UID\""
        );

        Table::_delete(
            'Users',
            "UID = \"$this->UID\""
        );
    }
}