Subversion Repositories oidplus

Rev

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