Subversion Repositories oidplus

Rev

Rev 433 | Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * OIDplus 2.0
  5.  * Copyright 2019 - 2021 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. if (!defined('INSIDE_OIDPLUS')) die();
  21.  
  22. class OIDplusPageRaChangePassword extends OIDplusPagePluginRa {
  23.  
  24.         public function action($actionID, $params) {
  25.                 if ($actionID == 'change_ra_password') {
  26.                         $email = $params['email'];
  27.  
  28.                         $res = OIDplus::db()->query("select * from ###ra where email = ?", array($email));
  29.                         if ($res->num_rows() == 0) {
  30.                                 throw new OIDplusException(_L('RA does not exist'));
  31.                         }
  32.  
  33.                         if (!OIDplus::authUtils()::isRaLoggedIn($email) && !OIDplus::authUtils()::isAdminLoggedIn()) {
  34.                                 throw new OIDplusException(_L('Authentication error. Please log in as admin, or as the RA to update its data.'));
  35.                         }
  36.  
  37.                         if (!OIDplus::authUtils()::isAdminLoggedIn()) {
  38.                                 $old_password = $params['old_password'];
  39.                         }
  40.                         $password1 = $params['new_password1'];
  41.                         $password2 = $params['new_password2'];
  42.  
  43.                         if ($password1 !== $password2) {
  44.                                 throw new OIDplusException(_L('Passwords do not match'));
  45.                         }
  46.  
  47.                         if (strlen($password1) < OIDplus::config()->getValue('ra_min_password_length')) {
  48.                                 $minlen = OIDplus::config()->getValue('ra_min_password_length');
  49.                                 throw new OIDplusException(_L('New password is too short. Minimum password length: %1',$minlen));
  50.                         }
  51.  
  52.                         $ra = new OIDplusRA($email);
  53.                         if (!$ra->isPasswordLess()) {
  54.                                 if (!OIDplus::authUtils()::isAdminLoggedIn()) {
  55.                                         if (!$ra->checkPassword($old_password)) {
  56.                                                 throw new OIDplusException(_L('Old password incorrect'));
  57.                                         }
  58.                                 }
  59.                                 OIDplus::logger()->log("[?WARN/!OK]RA($email)?/[?INFO/!OK]A?", "Password of RA '$email' changed");
  60.                         } else {
  61.                                 OIDplus::logger()->log("[?WARN/!OK]RA($email)?/[?INFO/!OK]A?", "Password of RA '$email' created");
  62.                         }
  63.                         $ra->change_password($password1);
  64.  
  65.                         return array("status" => 0);
  66.                 } else {
  67.                         throw new OIDplusException(_L('Unknown action ID'));
  68.                 }
  69.         }
  70.  
  71.         public function init($html=true) {
  72.                 // Nothing
  73.         }
  74.  
  75.         public function gui($id, &$out, &$handled) {
  76.                 if (explode('$',$id)[0] == 'oidplus:change_ra_password') {
  77.                         $handled = true;
  78.  
  79.                         $ra_email = explode('$',$id)[1];
  80.                         $ra = new OIDplusRA($ra_email);
  81.  
  82.                         $out['title'] = $ra->isPasswordLess() ? _L('Create password') : _L('Change RA password');
  83.                         $out['icon'] = file_exists(__DIR__.'/icon_big.png') ? OIDplus::webpath(__DIR__).'icon_big.png' : '';
  84.  
  85.                         if (!OIDplus::authUtils()::isRaLoggedIn($ra_email) && !OIDplus::authUtils()::isAdminLoggedIn()) {
  86.                                 $out['icon'] = 'img/error_big.png';
  87.                                 $out['text'] = '<p>'._L('You need to <a %1>log in</a> as the requested RA %2.',OIDplus::gui()->link('oidplus:login'),'<b>'.htmlentities($ra_email).'</b>').'</p>';
  88.                                 return;
  89.                         }
  90.  
  91.                         $res = OIDplus::db()->query("select * from ###ra where email = ?", array($ra_email));
  92.                         if ($res->num_rows() == 0) {
  93.                                 $out['icon'] = 'img/error_big.png';
  94.                                 $out['text'] = _L('RA "%1" does not exist','<b>'.htmlentities($ra_email).'</b>');
  95.                                 return;
  96.                         }
  97.  
  98.                         $out['text'] .= '<form id="raChangePasswordForm" action="javascript:void(0);" onsubmit="return raChangePasswordFormOnSubmit();">';
  99.                         $out['text'] .= '<input type="hidden" id="email" value="'.htmlentities($ra_email).'"/><br>';
  100.                         $out['text'] .= '<div><label class="padding_label">'._L('E-Mail').':</label><b>'.htmlentities($ra_email).'</b></div>';
  101.                         if (!$ra->isPasswordLess()) {
  102.                                 if (OIDplus::authUtils()::isAdminLoggedIn()) {
  103.                                         $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>';
  104.                                 } else {
  105.                                         $out['text'] .= '<div><label class="padding_label">'._L('Old password').':</label><input type="password" id="old_password" value=""/></div>';
  106.                                 }
  107.                         }
  108.                         $out['text'] .= '<div><label class="padding_label">'._L('New password').':</label><input type="password" id="new_password1" value=""/></div>';
  109.                         $out['text'] .= '<div><label class="padding_label">'._L('Repeat').':</label><input type="password" id="new_password2" value=""/></div>';
  110.                         $out['text'] .= '<br><input type="submit" value="'.($ra->isPasswordLess() ? _L('Create password') : _L('Change password')).'"></form>';
  111.                 }
  112.         }
  113.  
  114.         public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
  115.                 if (!$ra_email) return false;
  116.                 if (!OIDplus::authUtils()::isRaLoggedIn($ra_email) && !OIDplus::authUtils()::isAdminLoggedIn()) return false;
  117.  
  118.                 if (file_exists(__DIR__.'/treeicon.png')) {
  119.                         $tree_icon = OIDplus::webpath(__DIR__).'treeicon.png';
  120.                 } else {
  121.                         $tree_icon = null; // default icon (folder)
  122.                 }
  123.  
  124.                 $ra = new OIDplusRA($ra_email);
  125.  
  126.                 $json[] = array(
  127.                         'id' => 'oidplus:change_ra_password$'.$ra_email,
  128.                         'icon' => $tree_icon,
  129.                         'text' => $ra->isPasswordLess() ? _L('Create password') : _L('Change password')
  130.                 );
  131.  
  132.                 return true;
  133.         }
  134.  
  135.         public function tree_search($request) {
  136.                 return false;
  137.         }
  138. }
  139.