/
ConfigsSetCommand.php
220 lines (189 loc) · 6.28 KB
/
ConfigsSetCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
# MantisBT - A PHP based bugtracking system
# MantisBT is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# MantisBT is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with MantisBT. If not, see <http://www.gnu.org/licenses/>.
use Mantis\Exceptions\ClientException;
/**
* A command that sets config options.
*
* Only config options that can be overriden in the database can be set via this command.
*/
class ConfigsSetCommand extends Command {
/**
* Array of option names
*/
private $options;
/**
* The project id to retrieve options for, or ALL_PROJECTS.
*/
private $project_id;
/**
* The user id to retrieve options for, or ALL_USERS
*/
private $user_id;
/**
* Constructor
*
* @param array $p_data The command data.
*/
function __construct( array $p_data ) {
parent::__construct( $p_data );
}
/**
* Validate the data.
*
* @return void
*/
function validate() {
$t_current_user_id = auth_get_current_user_id();
// verify that user has appropriate access level to set configuration via database
$t_access_level_required = config_get_global( 'set_configuration_threshold' );
if( !access_has_global_level( $t_access_level_required, $t_current_user_id ) ) {
throw new ClientException(
"User doesn't have access to set configs.",
ERROR_ACCESS_DENIED );
}
$t_payload = $this->data['payload'];
// parse user id from payload, if not provided default to ALL_USERS
if( isset( $t_payload['user'] ) ) {
$this->user_id = mci_get_user_id( $t_payload['user'] );
} else {
$this->user_id = ALL_USERS;
}
// parse project id from payload, if not provided default to ALL_PROJECTS
if( isset( $t_payload['project'] ) ) {
$this->project_id = mci_get_project_id( $t_payload['project'] );
} else {
$this->project_id = ALL_PROJECTS;
}
if( $this->project_id != ALL_PROJECTS && !project_exists( $this->project_id ) ) {
throw new ClientException(
"Project doesn't exist",
ERROR_PROJECT_NOT_FOUND,
array( $this->project_id ) );
}
# This check is redundant if command is limited to administrator, but it is
# better to have it as a safe guard is non-administrators can change their own
# settings later.
if( $this->project_id != ALL_PROJECTS &&
$this->user_id != ALL_USERS &&
!access_has_project_level( VIEWER, $this->project_id, $this->user_id ) ) {
throw new ClientException(
"User doesn't have access to specified project.",
ERROR_ACCESS_DENIED );
}
$t_set_of_configs = $this->payload( 'configs' );
foreach( $t_set_of_configs as $t_config ) {
if( !isset( $t_config['option'] ) || is_blank( $t_config['option']) ) {
throw new ClientException(
'Config option not provided',
ERROR_EMPTY_FIELD,
array( 'option' ) );
};
$t_name = $t_config['option'];
# Silently ignore unknown configs - similar to get configs. This may be useful for
# compatibility with different MantisBT versions.
if( !config_is_set( $t_name ) ) {
continue;
}
# these are config options that are stored in the database, but can't be deleted
# or modified. For example, database_version (schema version).
if( !config_can_delete( $t_name ) ) {
continue;
}
# It is not allowed to set configs that are global and don't support db overrides
if( !config_can_set_in_database( $t_name ) ) {
throw new ClientException(
sprintf( "Config '%s' is global and cannot be set", $t_name ),
ERROR_INVALID_FIELD_VALUE,
array( $t_name ) );
}
if( !isset( $t_config['value'] ) ) {
$t_config['value'] = null;
}
if( ConfigsSetCommand::config_is_enum( $t_name ) &&
is_array( $t_config['value'] ) ) {
$t_config['value'] = ConfigsSetCommand::array_to_enum_string( $t_name, $t_config['value'] );
}
$this->options[] = $t_config;
}
}
/**
* Process the command.
*
* @returns void
*/
protected function process() {
foreach( $this->options as $t_option ) {
if( is_null( $t_option['value'] ) ) {
config_delete( $t_option['option'], $this->user_id, $this->project_id );
} else {
config_set( $t_option['option'], $t_option['value'], $this->user_id, $this->project_id );
}
}
}
/**
* Checks if the specific config option is an enum.
*
* @param string $p_option The option name.
* @return bool true enum, false otherwise.
*/
private static function config_is_enum( $p_option ) {
return stripos( $p_option, '_enum_string' ) !== false;
}
/**
* Convert an enum array into an enum string representation.
*
* Input:
* - array of enum entries. Each enum entry has an id and name.
* - Note that label (localized name) is not settable and hence not expected.
*/
private static function array_to_enum_string( $p_enum_name, $p_enum_array ) {
$t_enum_string = '';
foreach( $p_enum_array as $t_entry ) {
if( !isset( $t_entry['id'] ) || !isset( $t_entry['name'] ) ) {
throw new ClientException(
sprintf( "Enum '%s' missing 'id' or 'name' field for an entry", $p_enum_name ),
ERROR_INVALID_FIELD_VALUE,
array( $p_enum_name )
);
}
if( !is_numeric( $t_entry['id'] ) ) {
throw new ClientException(
sprintf( "Enum '%s' has 'id' that is not numeric", $p_enum_name ),
ERROR_INVALID_FIELD_VALUE,
array( $p_enum_name )
);
}
if( isset( $t_entry['label'] ) ) {
throw new ClientException(
sprintf( "Enum '%s' has 'label' property which is not supported", $p_enum_name ),
ERROR_INVALID_FIELD_VALUE,
array( $p_enum_name )
);
}
if( !preg_match('/^[a-zA-Z0-9_-]+$/', $t_entry['name'] ) ) {
throw new ClientException(
sprintf( "Enum '%s' has invalid enum entry name '%s'.", $p_enum_name, $t_entry['name'] ),
ERROR_INVALID_FIELD_VALUE,
array( $p_enum_name )
);
}
if( !empty( $t_enum_string ) ) {
$t_enum_string .= ',';
}
$t_enum_string .= (int)$t_entry['id'] . ':' . $t_entry['name'];
}
return $t_enum_string;
}
}