Subversion Repositories oidplus

Rev

Rev 1266 | Rev 1278 | 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()) {
1266 daniel-mar 37
                                throw new OIDplusHtmlException(_L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login$admin')), null, 401);
635 daniel-mar 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
 
1267 daniel-mar 66
                        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 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()) {
1266 daniel-mar 103
                                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 104
                        }
105
 
106
                        $out['text'] .= '<form id="adminCreateRaFrom" action="javascript:void(0);" onsubmit="return OIDplusPageAdminCreateRa.adminCreateRaFormOnSubmit();">';
676 daniel-mar 107
                        $out['text'] .= '<div><label class="padding_label">'._L('E-Mail').':</label><input type="text" id="email" value="'.htmlentities($email).'"></div>';
635 daniel-mar 108
                        $out['text'] .= '<div><label class="padding_label">'._L('Password').':</label><input type="password" id="password1" value=""/></div>';
109
                        $out['text'] .= '<div><label class="padding_label">'._L('Repeat').':</label><input type="password" id="password2" value=""/></div>';
110
                        $out['text'] .= '<br><input type="submit" value="'._L('Create').'"></form>';
111
                }
112
        }
113
 
1116 daniel-mar 114
        /**
115
         * @param array $json
116
         * @param string|null $ra_email
117
         * @param bool $nonjs
118
         * @param string $req_goto
119
         * @return bool
120
         * @throws OIDplusException
121
         */
122
        public function tree(array &$json, string $ra_email=null, bool $nonjs=false, string $req_goto=''): bool {
635 daniel-mar 123
                if (!OIDplus::authUtils()->isAdminLoggedIn()) return false;
124
 
800 daniel-mar 125
                if (file_exists(__DIR__.'/img/main_icon16.png')) {
801 daniel-mar 126
                        $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png';
635 daniel-mar 127
                } else {
128
                        $tree_icon = null; // default icon (folder)
129
                }
130
 
131
                $json[] = array(
132
                        'id' => 'oidplus:create_ra',
133
                        'icon' => $tree_icon,
134
                        'text' => _L('Create RA manually')
135
                );
136
 
137
                return true;
138
        }
139
 
1116 daniel-mar 140
        /**
141
         * @param string $request
142
         * @return array|false
143
         */
144
        public function tree_search(string $request) {
635 daniel-mar 145
                return false;
146
        }
147
}