Subversion Repositories oidplus

Rev

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