Subversion Repositories oidplus

Rev

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