Subversion Repositories oidplus

Rev

Rev 1266 | 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 OIDplusPageRaChangePassword extends OIDplusPagePluginRa {
  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 == 'change_ra_password') {
  36.                         _CheckParamExists($params, 'email');
  37.  
  38.                         $email = $params['email'];
  39.  
  40.                         $res = OIDplus::db()->query("select * from ###ra where email = ?", array($email));
  41.                         if (!$res->any()) {
  42.                                 throw new OIDplusException(_L('RA does not exist'));
  43.                         }
  44.  
  45.                         if (!OIDplus::authUtils()->isRaLoggedIn($email) && !OIDplus::authUtils()->isAdminLoggedIn()) {
  46.                                 throw new OIDplusException(_L('Authentication error. Please log in as admin, or as the RA to update its data.'), null, 401);
  47.                         }
  48.  
  49.                         if (!OIDplus::authUtils()->isAdminLoggedIn()) {
  50.                                 _CheckParamExists($params, 'old_password');
  51.                                 $old_password = $params['old_password'];
  52.                         } else {
  53.                                 $old_password = '';
  54.                         }
  55.  
  56.                         _CheckParamExists($params, 'new_password1');
  57.                         _CheckParamExists($params, 'new_password2');
  58.  
  59.                         $password1 = $params['new_password1'];
  60.                         $password2 = $params['new_password2'];
  61.  
  62.                         if ($password1 !== $password2) {
  63.                                 throw new OIDplusException(_L('Passwords do not match'));
  64.                         }
  65.  
  66.                         if (strlen($password1) < OIDplus::config()->getValue('ra_min_password_length')) {
  67.                                 $minlen = OIDplus::config()->getValue('ra_min_password_length');
  68.                                 throw new OIDplusException(_L('New password is too short. Minimum password length: %1',$minlen));
  69.                         }
  70.  
  71.                         $ra = new OIDplusRA($email);
  72.                         if (!$ra->isPasswordLess()) {
  73.                                 if (!OIDplus::authUtils()->isAdminLoggedIn()) {
  74.                                         if (!$ra->checkPassword($old_password)) {
  75.                                                 throw new OIDplusException(_L('Old password incorrect'));
  76.                                         }
  77.                                 }
  78.                                 OIDplus::logger()->log("V2:[OK/WARN]RA(%1)+[OK/INFO]A", "Password of RA '%1' changed", $email);
  79.                         } else {
  80.                                 OIDplus::logger()->log("V2:[OK/WARN]RA(%1)+[OK/INFO]A", "Password of RA '%1' created", $email);
  81.                         }
  82.                         $ra->change_password($password1);
  83.  
  84.                         return array("status" => 0);
  85.                 } else {
  86.                         return parent::action($actionID, $params);
  87.                 }
  88.         }
  89.  
  90.         /**
  91.          * @param bool $html
  92.          * @return void
  93.          */
  94.         public function init(bool $html=true) {
  95.                 // Nothing
  96.         }
  97.  
  98.         /**
  99.          * @param string $id
  100.          * @param array $out
  101.          * @param bool $handled
  102.          * @return void
  103.          * @throws OIDplusException
  104.          */
  105.         public function gui(string $id, array &$out, bool &$handled) {
  106.                 if (explode('$',$id)[0] == 'oidplus:change_ra_password') {
  107.                         $handled = true;
  108.  
  109.                         $ra_email = explode('$',$id)[1];
  110.                         $ra = new OIDplusRA($ra_email);
  111.  
  112.                         $out['title'] = $ra->isPasswordLess() ? _L('Create password') : _L('Change RA password');
  113.                         $out['icon'] = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
  114.  
  115.                         if (!OIDplus::authUtils()->isRaLoggedIn($ra_email) && !OIDplus::authUtils()->isAdminLoggedIn()) {
  116.                                 throw new OIDplusHtmlException(_L('You need to <a %1>log in</a> as the requested RA %2.',OIDplus::gui()->link('oidplus:login$ra$'.$ra_email),'<b>'.htmlentities($ra_email).'</b>'), $out['title'], 401);
  117.                         }
  118.  
  119.                         $res = OIDplus::db()->query("select * from ###ra where email = ?", array($ra_email));
  120.                         if (!$res->any()) {
  121.                                 throw new OIDplusHtmlException(_L('RA "%1" does not exist','<b>'.htmlentities($ra_email).'</b>'), $out['title']);
  122.                         }
  123.  
  124.                         $out['text'] .= '<form id="raChangePasswordForm" action="javascript:void(0);" onsubmit="return OIDplusPageRaChangePassword.raChangePasswordFormOnSubmit();">';
  125.                         $out['text'] .= '<input type="hidden" id="email" value="'.htmlentities($ra_email).'"/><br>';
  126.                         $out['text'] .= '<div><label class="padding_label">'._L('E-Mail').':</label><b>'.htmlentities($ra_email).'</b></div>';
  127.                         if (!$ra->isPasswordLess()) {
  128.                                 if (OIDplus::authUtils()->isAdminLoggedIn()) {
  129.                                         $out['text'] .= '<div><label class="padding_label">'._L('Old password').':</label><i>'._L('Admin can change the password without verification of the old password.').'</i></div>';
  130.                                 } else {
  131.                                         $out['text'] .= '<div><label class="padding_label">'._L('Old password').':</label><input type="password" id="old_password" value=""/></div>';
  132.                                 }
  133.                         }
  134.                         $out['text'] .= '<div><label class="padding_label">'._L('New password').':</label><input type="password" id="new_password1" value=""/></div>';
  135.                         $out['text'] .= '<div><label class="padding_label">'._L('Repeat').':</label><input type="password" id="new_password2" value=""/></div>';
  136.                         $out['text'] .= '<br><input type="submit" value="'.($ra->isPasswordLess() ? _L('Create password') : _L('Change password')).'"></form>';
  137.                 }
  138.         }
  139.  
  140.         /**
  141.          * @param array $json
  142.          * @param string|null $ra_email
  143.          * @param bool $nonjs
  144.          * @param string $req_goto
  145.          * @return bool
  146.          * @throws OIDplusException
  147.          */
  148.         public function tree(array &$json, string $ra_email=null, bool $nonjs=false, string $req_goto=''): bool {
  149.                 if (!$ra_email) return false;
  150.                 if (!OIDplus::authUtils()->isRaLoggedIn($ra_email) && !OIDplus::authUtils()->isAdminLoggedIn()) return false;
  151.  
  152.                 if (file_exists(__DIR__.'/img/main_icon16.png')) {
  153.                         $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png';
  154.                 } else {
  155.                         $tree_icon = null; // default icon (folder)
  156.                 }
  157.  
  158.                 $ra = new OIDplusRA($ra_email);
  159.  
  160.                 $json[] = array(
  161.                         'id' => 'oidplus:change_ra_password$'.$ra_email,
  162.                         'icon' => $tree_icon,
  163.                         'text' => $ra->isPasswordLess() ? _L('Create password') : _L('Change password')
  164.                 );
  165.  
  166.                 return true;
  167.         }
  168.  
  169.         /**
  170.          * @param string $request
  171.          * @return array|false
  172.          */
  173.         public function tree_search(string $request) {
  174.                 return false;
  175.         }
  176. }
  177.