Rev 1278 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
635 | daniel-mar | 1 | <?php |
2 | |||
3 | /* |
||
4 | * OIDplus 2.0 |
||
1086 | daniel-mar | 5 | * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft |
635 | daniel-mar | 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 | |||
1050 | daniel-mar | 20 | namespace ViaThinkSoft\OIDplus; |
635 | daniel-mar | 21 | |
1086 | daniel-mar | 22 | // phpcs:disable PSR1.Files.SideEffects |
23 | \defined('INSIDE_OIDPLUS') or die; |
||
24 | // phpcs:enable PSR1.Files.SideEffects |
||
25 | |||
635 | daniel-mar | 26 | class OIDplusPageAdminCreateRa extends OIDplusPagePluginAdmin { |
27 | |||
1293 | daniel-mar | 28 | |
1116 | daniel-mar | 29 | /** |
30 | * @param array $params |
||
1143 | daniel-mar | 31 | * @return array |
1116 | daniel-mar | 32 | * @throws OIDplusException |
33 | */ |
||
1293 | daniel-mar | 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 | } |
||
635 | daniel-mar | 38 | |
1293 | daniel-mar | 39 | _CheckParamExists($params, 'email'); |
40 | _CheckParamExists($params, 'password1'); |
||
41 | _CheckParamExists($params, 'password2'); |
||
635 | daniel-mar | 42 | |
1293 | daniel-mar | 43 | $email = $params['email']; |
44 | $password1 = $params['password1']; |
||
45 | $password2 = $params['password2']; |
||
635 | daniel-mar | 46 | |
1293 | daniel-mar | 47 | if (!OIDplus::mailUtils()->validMailAddress($email)) { |
48 | throw new OIDplusException(_L('eMail address is invalid.')); |
||
49 | } |
||
635 | daniel-mar | 50 | |
1293 | daniel-mar | 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 | } |
||
635 | daniel-mar | 55 | |
1293 | daniel-mar | 56 | if ($password1 !== $password2) { |
57 | throw new OIDplusException(_L('Passwords do not match')); |
||
58 | } |
||
635 | daniel-mar | 59 | |
1293 | daniel-mar | 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 | } |
||
635 | daniel-mar | 64 | |
1293 | daniel-mar | 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); |
635 | daniel-mar | 66 | |
1293 | daniel-mar | 67 | $ra = new OIDplusRA($email); |
68 | $ra->register_ra($password1); |
||
635 | daniel-mar | 69 | |
1293 | daniel-mar | 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); |
||
635 | daniel-mar | 82 | } else { |
1116 | daniel-mar | 83 | return parent::action($actionID, $params); |
635 | daniel-mar | 84 | } |
85 | } |
||
86 | |||
1116 | daniel-mar | 87 | /** |
88 | * @param bool $html |
||
89 | * @return void |
||
90 | */ |
||
91 | public function init(bool $html=true) { |
||
635 | daniel-mar | 92 | // Nothing |
93 | } |
||
94 | |||
1116 | daniel-mar | 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) { |
||
1278 | daniel-mar | 103 | $parts = explode('$',$id,2); |
676 | daniel-mar | 104 | $id = $parts[0]; |
1278 | daniel-mar | 105 | $email = $parts[1] ?? ''; |
676 | daniel-mar | 106 | |
635 | daniel-mar | 107 | if ($id == 'oidplus:create_ra') { |
108 | $handled = true; |
||
1278 | daniel-mar | 109 | |
635 | daniel-mar | 110 | $out['title'] = _L('Manual creation of a RA'); |
801 | daniel-mar | 111 | $out['icon'] = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : ''; |
635 | daniel-mar | 112 | |
113 | if (!OIDplus::authUtils()->isAdminLoggedIn()) { |
||
1266 | daniel-mar | 114 | throw new OIDplusHtmlException(_L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login$admin')), $out['title'], 401); |
635 | daniel-mar | 115 | } |
116 | |||
117 | $out['text'] .= '<form id="adminCreateRaFrom" action="javascript:void(0);" onsubmit="return OIDplusPageAdminCreateRa.adminCreateRaFormOnSubmit();">'; |
||
676 | daniel-mar | 118 | $out['text'] .= '<div><label class="padding_label">'._L('E-Mail').':</label><input type="text" id="email" value="'.htmlentities($email).'"></div>'; |
635 | daniel-mar | 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 | |||
1116 | daniel-mar | 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 { |
||
635 | daniel-mar | 134 | if (!OIDplus::authUtils()->isAdminLoggedIn()) return false; |
135 | |||
800 | daniel-mar | 136 | if (file_exists(__DIR__.'/img/main_icon16.png')) { |
801 | daniel-mar | 137 | $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png'; |
635 | daniel-mar | 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 | |||
1116 | daniel-mar | 151 | /** |
152 | * @param string $request |
||
153 | * @return array|false |
||
154 | */ |
||
155 | public function tree_search(string $request) { |
||
635 | daniel-mar | 156 | return false; |
157 | } |
||
158 | } |