Subversion Repositories oidplus

Rev

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

  1. <?php
  2.  
  3. /*
  4.  * OIDplus 2.0
  5.  * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft
  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.  
  20. namespace ViaThinkSoft\OIDplus;
  21.  
  22. // phpcs:disable PSR1.Files.SideEffects
  23. \defined('INSIDE_OIDPLUS') or die;
  24. // phpcs:enable PSR1.Files.SideEffects
  25.  
  26. class OIDplusRA extends OIDplusBaseClass {
  27.  
  28.         /**
  29.          * @var string
  30.          */
  31.         private $email = null;
  32.  
  33.         /**
  34.          * @param string $email
  35.          */
  36.         public function __construct(string $email) {
  37.                 $this->email = $email;
  38.         }
  39.  
  40.         /**
  41.          * @return string
  42.          */
  43.         public function raEmail(): string {
  44.                 return $this->email;
  45.         }
  46.  
  47.         /**
  48.          * @return bool
  49.          * @throws OIDplusException
  50.          */
  51.         public function existing(): bool {
  52.                 $res = OIDplus::db()->query("select email from ###ra where email = ?", array($this->email));
  53.                 return ($res->any());
  54.         }
  55.  
  56.         /**
  57.          * @return string
  58.          * @throws OIDplusException
  59.          */
  60.         public function raName(): string {
  61.                 $res = OIDplus::db()->query("select ra_name from ###ra where email = ?", array($this->email));
  62.                 if (!$res->any()) return _L('(RA not in database)');
  63.                 $row = $res->fetch_array();
  64.                 return $row['ra_name'] ?? '';
  65.         }
  66.  
  67.         /**
  68.          * @return OIDplusRA[]
  69.          * @throws OIDplusException
  70.          */
  71.         public static function getAllRAs(): array {
  72.                 $out = array();
  73.                 $res = OIDplus::db()->query("select email from ###ra");
  74.                 while ($row = $res->fetch_array()) {
  75.                         $out[] = new OIDplusRA($row['email'] ?? '');
  76.                 }
  77.                 return $out;
  78.         }
  79.  
  80.         /**
  81.          * @param string $new_password
  82.          * @return void
  83.          * @throws OIDplusException
  84.          */
  85.         public function change_password(string $new_password) {
  86.                 $authInfo = OIDplus::authUtils()->raGeneratePassword($new_password);
  87.                 $calc_authkey = $authInfo->getAuthKey();
  88.                 OIDplus::db()->query("update ###ra set authkey=? where email = ?", array($calc_authkey, $this->email));
  89.         }
  90.  
  91.         /**
  92.          * @param string $new_email
  93.          * @return void
  94.          * @throws OIDplusException
  95.          */
  96.         public function change_email(string $new_email) {
  97.                 OIDplus::db()->query("update ###ra set email = ? where email = ?", array($new_email, $this->email));
  98.         }
  99.  
  100.         /**
  101.          * @param string|null $new_password
  102.          * @return void
  103.          * @throws OIDplusException
  104.          */
  105.         public function register_ra(string $new_password=null) {
  106.                 if (is_null($new_password)) {
  107.                         // Invalid password (used for LDAP/OAuth)
  108.                         $calc_authkey = '';
  109.                 } else {
  110.                         $authInfo = OIDplus::authUtils()->raGeneratePassword($new_password);
  111.                         $calc_authkey = $authInfo->getAuthKey();
  112.                 }
  113.  
  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, "", "", "", "", "", "", "", "", "", ""));
  115.         }
  116.  
  117.         /**
  118.          * @return OIDplusRAAuthInfo|null
  119.          * @throws OIDplusException
  120.          */
  121.         public function getAuthInfo()/*: ?OIDplusRAAuthInfo*/ {
  122.                 $ra_res = OIDplus::db()->query("select authkey from ###ra where email = ?", array($this->email));
  123.                 if (!$ra_res->any()) return null; // User not found
  124.                 $ra_row = $ra_res->fetch_array();
  125.  
  126.                 return new OIDplusRAAuthInfo($ra_row['authkey']);
  127.         }
  128.  
  129.         /**
  130.          * @param string $password
  131.          * @return bool
  132.          * @throws OIDplusException
  133.          */
  134.         public function checkPassword(string $password): bool {
  135.                 return OIDplus::authUtils()->raCheckPassword($this->email, $password);
  136.         }
  137.  
  138.         /**
  139.          * @return void
  140.          * @throws OIDplusException
  141.          */
  142.         public function delete() {
  143.                 OIDplus::db()->query("delete from ###ra where email = ?", array($this->email));
  144.         }
  145.  
  146.         /**
  147.          * @param string $ra_name
  148.          * @return void
  149.          * @throws OIDplusException
  150.          */
  151.         public function setRaName(string $ra_name) {
  152.                 OIDplus::db()->query("update ###ra set ra_name = ? where email = ?", array($ra_name, $this->email));
  153.         }
  154.  
  155.         /**
  156.          * @return bool|null
  157.          * @throws OIDplusException
  158.          */
  159.         public function isPasswordLess()/*: ?bool*/ {
  160.                 $authInfo = $this->getAuthInfo();
  161.                 if (!$authInfo) return null; // user not found
  162.                 return $authInfo->isPasswordLess();
  163.         }
  164. }
  165.