Subversion Repositories oidplus

Rev

Rev 1130 | Go to most recent revision | Blame | 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. abstract class OIDplusAuthContentStore extends OIDplusBaseClass implements OIDplusGetterSetterInterface {
  27.  
  28.         // Getter / Setter
  29.  
  30.         /**
  31.          * @param string $name
  32.          * @param mixed|null $default
  33.          * @return mixed|null
  34.          */
  35.         public abstract function getValue(string $name, $default = NULL);
  36.  
  37.         /**
  38.          * @param string $name
  39.          * @param mixed $value
  40.          * @return void
  41.          */
  42.         public abstract function setValue(string $name, $value);
  43.  
  44.         /**
  45.          * @param string $name
  46.          * @return bool
  47.          */
  48.         public abstract function exists(string $name): bool;
  49.  
  50.         /**
  51.          * @param string $name
  52.          * @return void
  53.          */
  54.         public abstract function delete(string $name);
  55.  
  56.         /**
  57.          * @return OIDplusAuthContentStore|null
  58.          * @throws OIDplusException
  59.          */
  60.         public abstract static function getActiveProvider()/*: ?OIDplusAuthContentStore*/;
  61.  
  62.         /**
  63.          * @return mixed
  64.          */
  65.         public abstract function destroySession();
  66.  
  67.         /**
  68.          * @return mixed
  69.          */
  70.         public abstract function activate();
  71.  
  72.         /**
  73.          * @param string $email
  74.          * @param string $loginfo
  75.          * @return void
  76.          */
  77.         public abstract function raLoginEx(string $email, string &$loginfo);
  78.  
  79.         /**
  80.          * @param string $email
  81.          * @param string $loginfo
  82.          * @return void
  83.          */
  84.         public abstract function raLogoutEx(string $email, string &$loginfo);
  85.  
  86.         /**
  87.          * @param string $loginfo
  88.          * @return void
  89.          */
  90.         public abstract function adminLoginEx(string &$loginfo);
  91.  
  92.         /**
  93.          * @param string $loginfo
  94.          * @return void
  95.          */
  96.         public abstract function adminLogoutEx(string &$loginfo);
  97.  
  98.         // RA authentication functions (low-level)
  99.  
  100.         /**
  101.          * @param string $email
  102.          * @return void
  103.          */
  104.         public function raLogin(string $email) {
  105.                 if (strpos($email, '|') !== false) return;
  106.  
  107.                 $list = $this->getValue('oidplus_ra_logged_in');
  108.                 if (is_null($list)) $list = '';
  109.  
  110.                 $ary = ($list == '') ? array() : explode('|', $list);
  111.                 if (!in_array($email, $ary)) $ary[] = $email;
  112.                 $list = implode('|', $ary);
  113.  
  114.                 $this->setValue('oidplus_ra_logged_in', $list);
  115.         }
  116.  
  117.         /**
  118.          * @param string $email
  119.          * @return void
  120.          */
  121.         public function raLogout(string $email) {
  122.                 $list = $this->getValue('oidplus_ra_logged_in');
  123.                 if (is_null($list)) $list = '';
  124.  
  125.                 $ary = ($list == '') ? array() : explode('|', $list);
  126.                 $key = array_search($email, $ary);
  127.                 if ($key !== false) unset($ary[$key]);
  128.                 $list = implode('|', $ary);
  129.  
  130.                 $this->setValue('oidplus_ra_logged_in', $list);
  131.         }
  132.  
  133.         /**
  134.          * @return int
  135.          */
  136.         public function raNumLoggedIn(): int {
  137.                 return count($this->loggedInRaList());
  138.         }
  139.  
  140.         /**
  141.          * @return OIDplusRA[]
  142.          */
  143.         public function loggedInRaList(): array {
  144.                 $list = $this->getValue('oidplus_ra_logged_in');
  145.                 if (is_null($list)) $list = '';
  146.  
  147.                 $res = array();
  148.                 foreach (array_unique(explode('|',$list)) as $ra_email) {
  149.                         if ($ra_email == '') continue;
  150.                         $res[] = new OIDplusRA($ra_email);
  151.                 }
  152.                 return $res;
  153.         }
  154.  
  155.         /**
  156.          * @param string $email
  157.          * @return bool
  158.          */
  159.         public function isRaLoggedIn(string $email): bool {
  160.                 foreach ($this->loggedInRaList() as $ra) {
  161.                         if ($email == $ra->raEmail()) return true;
  162.                 }
  163.                 return false;
  164.         }
  165.  
  166.         // Admin authentication functions (low-level)
  167.  
  168.         /**
  169.          * @return void
  170.          */
  171.         public function adminLogin() {
  172.                 $this->setValue('oidplus_admin_logged_in', 1);
  173.         }
  174.  
  175.         /**
  176.          * @return void
  177.          */
  178.         public function adminLogout() {
  179.                 $this->setValue('oidplus_admin_logged_in', 0);
  180.         }
  181.  
  182.         /**
  183.          * @return bool
  184.          */
  185.         public function isAdminLoggedIn(): bool {
  186.                 return $this->getValue('oidplus_admin_logged_in', 0) == 1;
  187.         }
  188.  
  189. }
  190.