Subversion Repositories oidplus

Rev

Rev 1130 | Rev 1199 | 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 OIDplusPageAdminCreateRa extends OIDplusPagePluginAdmin {
  27.  
  28.         /**
  29.          * @param string $actionID
  30.          * @param array $params
  31.          * @return array
  32.          * @throws OIDplusException
  33.          */
  34.         public function action(string $actionID, array $params): array {
  35.                 if ($actionID == 'create_ra') {
  36.                         if (!OIDplus::authUtils()->isAdminLoggedIn()) {
  37.                                 throw new OIDplusException(_L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login$admin')));
  38.                         }
  39.  
  40.                         _CheckParamExists($params, 'email');
  41.                         _CheckParamExists($params, 'password1');
  42.                         _CheckParamExists($params, 'password2');
  43.  
  44.                         $email = $params['email'];
  45.                         $password1 = $params['password1'];
  46.                         $password2 = $params['password2'];
  47.  
  48.                         if (!OIDplus::mailUtils()->validMailAddress($email)) {
  49.                                 throw new OIDplusException(_L('eMail address is invalid.'));
  50.                         }
  51.  
  52.                         $res = OIDplus::db()->query("select * from ###ra where email = ?", array($email)); // TODO: this should be a static function in the RA class
  53.                         if ($res->any()) {
  54.                                 throw new OIDplusException(_L('RA does already exist'));
  55.                         }
  56.  
  57.                         if ($password1 !== $password2) {
  58.                                 throw new OIDplusException(_L('Passwords do not match'));
  59.                         }
  60.  
  61.                         if (strlen($password1) < OIDplus::config()->getValue('ra_min_password_length')) {
  62.                                 $minlen = OIDplus::config()->getValue('ra_min_password_length');
  63.                                 throw new OIDplusException(_L('Password is too short. Need at least %1 characters',$minlen));
  64.                         }
  65.  
  66.                         OIDplus::logger()->log("[INFO]RA($email)!/A?", "RA '$email' was created by the admin, without email address verification or invitation");
  67.  
  68.                         $ra = new OIDplusRA($email);
  69.                         $ra->register_ra($password1);
  70.  
  71.                         return array("status" => 0);
  72.                 } else {
  73.                         return parent::action($actionID, $params);
  74.                 }
  75.         }
  76.  
  77.         /**
  78.          * @param bool $html
  79.          * @return void
  80.          */
  81.         public function init(bool $html=true) {
  82.                 // Nothing
  83.         }
  84.  
  85.         /**
  86.          * @param string $id
  87.          * @param array $out
  88.          * @param bool $handled
  89.          * @return void
  90.          * @throws OIDplusException
  91.          */
  92.         public function gui(string $id, array &$out, bool &$handled) {
  93.                 $parts = explode('$',$id);
  94.                 $id = $parts[0];
  95.  
  96.                 if ($id == 'oidplus:create_ra') {
  97.                         $handled = true;
  98.                         $email = $parts[1] ?? '';
  99.                         $out['title'] = _L('Manual creation of a RA');
  100.                         $out['icon'] = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
  101.  
  102.                         if (!OIDplus::authUtils()->isAdminLoggedIn()) {
  103.                                 $out['icon'] = 'img/error.png';
  104.                                 $out['text'] = '<p>'._L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login$admin')).'</p>';
  105.                                 return;
  106.                         }
  107.  
  108.                         $out['text'] .= '<form id="adminCreateRaFrom" action="javascript:void(0);" onsubmit="return OIDplusPageAdminCreateRa.adminCreateRaFormOnSubmit();">';
  109.                         $out['text'] .= '<div><label class="padding_label">'._L('E-Mail').':</label><input type="text" id="email" value="'.htmlentities($email).'"></div>';
  110.                         $out['text'] .= '<div><label class="padding_label">'._L('Password').':</label><input type="password" id="password1" value=""/></div>';
  111.                         $out['text'] .= '<div><label class="padding_label">'._L('Repeat').':</label><input type="password" id="password2" value=""/></div>';
  112.                         $out['text'] .= '<br><input type="submit" value="'._L('Create').'"></form>';
  113.                 }
  114.         }
  115.  
  116.         /**
  117.          * @param array $json
  118.          * @param string|null $ra_email
  119.          * @param bool $nonjs
  120.          * @param string $req_goto
  121.          * @return bool
  122.          * @throws OIDplusException
  123.          */
  124.         public function tree(array &$json, string $ra_email=null, bool $nonjs=false, string $req_goto=''): bool {
  125.                 if (!OIDplus::authUtils()->isAdminLoggedIn()) return false;
  126.  
  127.                 if (file_exists(__DIR__.'/img/main_icon16.png')) {
  128.                         $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png';
  129.                 } else {
  130.                         $tree_icon = null; // default icon (folder)
  131.                 }
  132.  
  133.                 $json[] = array(
  134.                         'id' => 'oidplus:create_ra',
  135.                         'icon' => $tree_icon,
  136.                         'text' => _L('Create RA manually')
  137.                 );
  138.  
  139.                 return true;
  140.         }
  141.  
  142.         /**
  143.          * @param string $request
  144.          * @return array|false
  145.          */
  146.         public function tree_search(string $request) {
  147.                 return false;
  148.         }
  149. }
  150.