Subversion Repositories oidplus

Rev

Rev 1119 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 daniel-mar 1
<?php
2
 
3
/*
4
 * OIDplus 2.0
1086 daniel-mar 5
 * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft
2 daniel-mar 6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with the License.
9
 * You may obtain a copy of the License at
10
 *
11
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
18
 */
19
 
1050 daniel-mar 20
namespace ViaThinkSoft\OIDplus;
511 daniel-mar 21
 
1086 daniel-mar 22
// phpcs:disable PSR1.Files.SideEffects
23
\defined('INSIDE_OIDPLUS') or die;
24
// phpcs:enable PSR1.Files.SideEffects
25
 
730 daniel-mar 26
class OIDplusRA extends OIDplusBaseClass {
1130 daniel-mar 27
 
28
        /**
29
         * @var string
30
         */
2 daniel-mar 31
        private $email = null;
32
 
1116 daniel-mar 33
        /**
34
         * @param string $email
35
         */
36
        public function __construct(string $email) {
2 daniel-mar 37
                $this->email = $email;
38
        }
39
 
1116 daniel-mar 40
        /**
41
         * @return string
42
         */
43
        public function raEmail(): string {
115 daniel-mar 44
                return $this->email;
45
        }
329 daniel-mar 46
 
1116 daniel-mar 47
        /**
48
         * @return bool
49
         * @throws OIDplusException
50
         */
51
        public function existing(): bool {
261 daniel-mar 52
                $res = OIDplus::db()->query("select email from ###ra where email = ?", array($this->email));
790 daniel-mar 53
                return ($res->any());
253 daniel-mar 54
        }
115 daniel-mar 55
 
1116 daniel-mar 56
        /**
57
         * @return string
58
         * @throws OIDplusException
59
         */
60
        public function raName(): string {
261 daniel-mar 61
                $res = OIDplus::db()->query("select ra_name from ###ra where email = ?", array($this->email));
790 daniel-mar 62
                if (!$res->any()) return _L('(RA not in database)');
236 daniel-mar 63
                $row = $res->fetch_array();
1119 daniel-mar 64
                return $row['ra_name'] ?? '';
2 daniel-mar 65
        }
66
 
1116 daniel-mar 67
        /**
68
         * @return OIDplusRA[]
69
         * @throws OIDplusException
70
         */
71
        public static function getAllRAs(): array {
115 daniel-mar 72
                $out = array();
261 daniel-mar 73
                $res = OIDplus::db()->query("select email from ###ra");
236 daniel-mar 74
                while ($row = $res->fetch_array()) {
1119 daniel-mar 75
                        $out[] = new OIDplusRA($row['email'] ?? '');
115 daniel-mar 76
                }
77
                return $out;
78
        }
79
 
1116 daniel-mar 80
        /**
81
         * @param string $new_password
82
         * @return void
83
         * @throws OIDplusException
84
         */
85
        public function change_password(string $new_password) {
459 daniel-mar 86
                $authInfo = OIDplus::authUtils()->raGeneratePassword($new_password);
87
                $calc_authkey = $authInfo->getAuthKey();
1090 daniel-mar 88
                OIDplus::db()->query("update ###ra set authkey=? where email = ?", array($calc_authkey, $this->email));
2 daniel-mar 89
        }
90
 
1116 daniel-mar 91
        /**
92
         * @param string $new_email
93
         * @return void
94
         * @throws OIDplusException
95
         */
96
        public function change_email(string $new_email) {
261 daniel-mar 97
                OIDplus::db()->query("update ###ra set email = ? where email = ?", array($new_email, $this->email));
44 daniel-mar 98
        }
99
 
1116 daniel-mar 100
        /**
101
         * @param string|null $new_password
102
         * @return void
103
         * @throws OIDplusException
104
         */
1130 daniel-mar 105
        public function register_ra(string $new_password=null) {
430 daniel-mar 106
                if (is_null($new_password)) {
107
                        // Invalid password (used for LDAP/OAuth)
108
                        $calc_authkey = '';
109
                } else {
459 daniel-mar 110
                        $authInfo = OIDplus::authUtils()->raGeneratePassword($new_password);
111
                        $calc_authkey = $authInfo->getAuthKey();
430 daniel-mar 112
                }
113
 
1090 daniel-mar 114
                OIDplus::db()->query("insert into ###ra (authkey, email, registered, ra_name, personal_name, organization, office, street, zip_town, country, phone, mobile, fax) values (?, ?, ".OIDplus::db()->sqlDate().", ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", array($calc_authkey, $this->email, "", "", "", "", "", "", "", "", "", ""));
2 daniel-mar 115
        }
116
 
1116 daniel-mar 117
        /**
118
         * @return OIDplusRAAuthInfo|null
119
         * @throws OIDplusException
120
         */
590 daniel-mar 121
        public function getAuthInfo()/*: ?OIDplusRAAuthInfo*/ {
1090 daniel-mar 122
                $ra_res = OIDplus::db()->query("select authkey from ###ra where email = ?", array($this->email));
790 daniel-mar 123
                if (!$ra_res->any()) return null; // User not found
236 daniel-mar 124
                $ra_row = $ra_res->fetch_array();
2 daniel-mar 125
 
1090 daniel-mar 126
                return new OIDplusRAAuthInfo($ra_row['authkey']);
453 daniel-mar 127
        }
2 daniel-mar 128
 
1116 daniel-mar 129
        /**
130
         * @param string $password
131
         * @return bool
132
         * @throws OIDplusException
133
         */
134
        public function checkPassword(string $password): bool {
453 daniel-mar 135
                return OIDplus::authUtils()->raCheckPassword($this->email, $password);
2 daniel-mar 136
        }
137
 
1116 daniel-mar 138
        /**
139
         * @return void
140
         * @throws OIDplusException
141
         */
115 daniel-mar 142
        public function delete() {
261 daniel-mar 143
                OIDplus::db()->query("delete from ###ra where email = ?", array($this->email));
2 daniel-mar 144
        }
145
 
1116 daniel-mar 146
        /**
147
         * @param string $ra_name
148
         * @return void
149
         * @throws OIDplusException
150
         */
151
        public function setRaName(string $ra_name) {
261 daniel-mar 152
                OIDplus::db()->query("update ###ra set ra_name = ? where email = ?", array($ra_name, $this->email));
2 daniel-mar 153
        }
433 daniel-mar 154
 
1116 daniel-mar 155
        /**
156
         * @return bool|null
157
         * @throws OIDplusException
158
         */
159
        public function isPasswordLess()/*: ?bool*/ {
590 daniel-mar 160
                $authInfo = $this->getAuthInfo();
161
                if (!$authInfo) return null; // user not found
162
                return $authInfo->isPasswordLess();
433 daniel-mar 163
        }
164
}