Subversion Repositories oidplus

Rev

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

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