Rev 1143 | Rev 1201 | Go to most recent revision | 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 | |||
1116 | daniel-mar | 28 | /** |
29 | * @param string $actionID |
||
30 | * @param array $params |
||
1143 | daniel-mar | 31 | * @return array |
1116 | daniel-mar | 32 | * @throws OIDplusException |
33 | */ |
||
34 | public function action(string $actionID, array $params): array { |
||
635 | daniel-mar | 35 | if ($actionID == 'create_ra') { |
36 | if (!OIDplus::authUtils()->isAdminLoggedIn()) { |
||
37 | throw new OIDplusException(_L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login$admin'))); |
||
38 | } |
||
39 | |||
40 | _CheckParamExists($params, 'email'); |
||
41 | _CheckParamExists($params, 'password1'); |
||
42 | _CheckParamExists($params, 'password2'); |
||
43 | |||
44 | $email = $params['email']; |
||
45 | $password1 = $params['password1']; |
||
46 | $password2 = $params['password2']; |
||
47 | |||
48 | if (!OIDplus::mailUtils()->validMailAddress($email)) { |
||
49 | throw new OIDplusException(_L('eMail address is invalid.')); |
||
50 | } |
||
51 | |||
52 | $res = OIDplus::db()->query("select * from ###ra where email = ?", array($email)); // TODO: this should be a static function in the RA class |
||
790 | daniel-mar | 53 | if ($res->any()) { |
635 | daniel-mar | 54 | throw new OIDplusException(_L('RA does already exist')); |
55 | } |
||
56 | |||
57 | if ($password1 !== $password2) { |
||
58 | throw new OIDplusException(_L('Passwords do not match')); |
||
59 | } |
||
60 | |||
61 | if (strlen($password1) < OIDplus::config()->getValue('ra_min_password_length')) { |
||
62 | $minlen = OIDplus::config()->getValue('ra_min_password_length'); |
||
63 | throw new OIDplusException(_L('Password is too short. Need at least %1 characters',$minlen)); |
||
64 | } |
||
65 | |||
1199 | daniel-mar | 66 | OIDplus::logger()->log("[INFO]RA(%1)!/A?", "RA '%1' was created by the admin, without email address verification or invitation", $email); |
635 | daniel-mar | 67 | |
68 | $ra = new OIDplusRA($email); |
||
69 | $ra->register_ra($password1); |
||
70 | |||
71 | return array("status" => 0); |
||
72 | } else { |
||
1116 | daniel-mar | 73 | return parent::action($actionID, $params); |
635 | daniel-mar | 74 | } |
75 | } |
||
76 | |||
1116 | daniel-mar | 77 | /** |
78 | * @param bool $html |
||
79 | * @return void |
||
80 | */ |
||
81 | public function init(bool $html=true) { |
||
635 | daniel-mar | 82 | // Nothing |
83 | } |
||
84 | |||
1116 | daniel-mar | 85 | /** |
86 | * @param string $id |
||
87 | * @param array $out |
||
88 | * @param bool $handled |
||
89 | * @return void |
||
90 | * @throws OIDplusException |
||
91 | */ |
||
92 | public function gui(string $id, array &$out, bool &$handled) { |
||
676 | daniel-mar | 93 | $parts = explode('$',$id); |
94 | $id = $parts[0]; |
||
95 | |||
635 | daniel-mar | 96 | if ($id == 'oidplus:create_ra') { |
97 | $handled = true; |
||
1130 | daniel-mar | 98 | $email = $parts[1] ?? ''; |
635 | daniel-mar | 99 | $out['title'] = _L('Manual creation of a RA'); |
801 | daniel-mar | 100 | $out['icon'] = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : ''; |
635 | daniel-mar | 101 | |
102 | if (!OIDplus::authUtils()->isAdminLoggedIn()) { |
||
800 | daniel-mar | 103 | $out['icon'] = 'img/error.png'; |
635 | daniel-mar | 104 | $out['text'] = '<p>'._L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login$admin')).'</p>'; |
105 | return; |
||
106 | } |
||
107 | |||
108 | $out['text'] .= '<form id="adminCreateRaFrom" action="javascript:void(0);" onsubmit="return OIDplusPageAdminCreateRa.adminCreateRaFormOnSubmit();">'; |
||
676 | daniel-mar | 109 | $out['text'] .= '<div><label class="padding_label">'._L('E-Mail').':</label><input type="text" id="email" value="'.htmlentities($email).'"></div>'; |
635 | daniel-mar | 110 | $out['text'] .= '<div><label class="padding_label">'._L('Password').':</label><input type="password" id="password1" value=""/></div>'; |
111 | $out['text'] .= '<div><label class="padding_label">'._L('Repeat').':</label><input type="password" id="password2" value=""/></div>'; |
||
112 | $out['text'] .= '<br><input type="submit" value="'._L('Create').'"></form>'; |
||
113 | } |
||
114 | } |
||
115 | |||
1116 | daniel-mar | 116 | /** |
117 | * @param array $json |
||
118 | * @param string|null $ra_email |
||
119 | * @param bool $nonjs |
||
120 | * @param string $req_goto |
||
121 | * @return bool |
||
122 | * @throws OIDplusException |
||
123 | */ |
||
124 | public function tree(array &$json, string $ra_email=null, bool $nonjs=false, string $req_goto=''): bool { |
||
635 | daniel-mar | 125 | if (!OIDplus::authUtils()->isAdminLoggedIn()) return false; |
126 | |||
800 | daniel-mar | 127 | if (file_exists(__DIR__.'/img/main_icon16.png')) { |
801 | daniel-mar | 128 | $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png'; |
635 | daniel-mar | 129 | } else { |
130 | $tree_icon = null; // default icon (folder) |
||
131 | } |
||
132 | |||
133 | $json[] = array( |
||
134 | 'id' => 'oidplus:create_ra', |
||
135 | 'icon' => $tree_icon, |
||
136 | 'text' => _L('Create RA manually') |
||
137 | ); |
||
138 | |||
139 | return true; |
||
140 | } |
||
141 | |||
1116 | daniel-mar | 142 | /** |
143 | * @param string $request |
||
144 | * @return array|false |
||
145 | */ |
||
146 | public function tree_search(string $request) { |
||
635 | daniel-mar | 147 | return false; |
148 | } |
||
149 | } |