Subversion Repositories oidplus

Rev

Rev 1050 | Rev 1090 | Go to most recent revision | 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 {
2 daniel-mar 27
        private $email = null;
28
 
115 daniel-mar 29
        public function __construct($email) {
2 daniel-mar 30
                $this->email = $email;
31
        }
32
 
115 daniel-mar 33
        public function raEmail() {
34
                return $this->email;
35
        }
329 daniel-mar 36
 
253 daniel-mar 37
        public function existing() {
261 daniel-mar 38
                $res = OIDplus::db()->query("select email from ###ra where email = ?", array($this->email));
790 daniel-mar 39
                return ($res->any());
253 daniel-mar 40
        }
115 daniel-mar 41
 
2 daniel-mar 42
        public function raName() {
261 daniel-mar 43
                $res = OIDplus::db()->query("select ra_name from ###ra where email = ?", array($this->email));
790 daniel-mar 44
                if (!$res->any()) return _L('(RA not in database)');
236 daniel-mar 45
                $row = $res->fetch_array();
2 daniel-mar 46
                return $row['ra_name'];
47
        }
48
 
115 daniel-mar 49
        public static function getAllRAs() {
50
                $out = array();
261 daniel-mar 51
                $res = OIDplus::db()->query("select email from ###ra");
236 daniel-mar 52
                while ($row = $res->fetch_array()) {
115 daniel-mar 53
                        $out[] = new OIDplusRA($row['email']);
54
                }
55
                return $out;
56
        }
57
 
58
        public function change_password($new_password) {
459 daniel-mar 59
                $authInfo = OIDplus::authUtils()->raGeneratePassword($new_password);
60
                $s_salt = $authInfo->getSalt();
61
                $calc_authkey = $authInfo->getAuthKey();
261 daniel-mar 62
                OIDplus::db()->query("update ###ra set salt=?, authkey=? where email = ?", array($s_salt, $calc_authkey, $this->email));
2 daniel-mar 63
        }
64
 
115 daniel-mar 65
        public function change_email($new_email) {
261 daniel-mar 66
                OIDplus::db()->query("update ###ra set email = ? where email = ?", array($new_email, $this->email));
44 daniel-mar 67
        }
68
 
115 daniel-mar 69
        public function register_ra($new_password) {
430 daniel-mar 70
                if (is_null($new_password)) {
71
                        // Invalid password (used for LDAP/OAuth)
72
                        $s_salt = '';
73
                        $calc_authkey = '';
74
                } else {
459 daniel-mar 75
                        $authInfo = OIDplus::authUtils()->raGeneratePassword($new_password);
76
                        $s_salt = $authInfo->getSalt();
77
                        $calc_authkey = $authInfo->getAuthKey();
430 daniel-mar 78
                }
79
 
329 daniel-mar 80
                OIDplus::db()->query("insert into ###ra (salt, authkey, email, registered, ra_name, personal_name, organization, office, street, zip_town, country, phone, mobile, fax) values (?, ?, ?, ".OIDplus::db()->sqlDate().", ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", array($s_salt, $calc_authkey, $this->email, "", "", "", "", "", "", "", "", "", ""));
2 daniel-mar 81
        }
82
 
590 daniel-mar 83
        public function getAuthInfo()/*: ?OIDplusRAAuthInfo*/ {
261 daniel-mar 84
                $ra_res = OIDplus::db()->query("select authkey, salt from ###ra where email = ?", array($this->email));
790 daniel-mar 85
                if (!$ra_res->any()) return null; // User not found
236 daniel-mar 86
                $ra_row = $ra_res->fetch_array();
2 daniel-mar 87
 
460 daniel-mar 88
                return new OIDplusRAAuthInfo($ra_row['salt'], $ra_row['authkey']);
453 daniel-mar 89
        }
2 daniel-mar 90
 
453 daniel-mar 91
        public function checkPassword($password) {
92
                return OIDplus::authUtils()->raCheckPassword($this->email, $password);
2 daniel-mar 93
        }
94
 
115 daniel-mar 95
        public function delete() {
261 daniel-mar 96
                OIDplus::db()->query("delete from ###ra where email = ?", array($this->email));
2 daniel-mar 97
        }
98
 
115 daniel-mar 99
        public function setRaName($ra_name) {
261 daniel-mar 100
                OIDplus::db()->query("update ###ra set ra_name = ? where email = ?", array($ra_name, $this->email));
2 daniel-mar 101
        }
433 daniel-mar 102
 
103
        public function isPasswordLess() {
590 daniel-mar 104
                $authInfo = $this->getAuthInfo();
105
                if (!$authInfo) return null; // user not found
106
                return $authInfo->isPasswordLess();
433 daniel-mar 107
        }
108
}