1<?php
2# MantisBT - A PHP based bugtracking system
3
4# MantisBT is free software: you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation, either version 2 of the License, or
7# (at your option) any later version.
8#
9# MantisBT is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with MantisBT.  If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Current User API
19 *
20 * @package CoreAPI
21 * @subpackage CurrentUserAPI
22 * @copyright Copyright 2000 - 2002  Kenzaburo Ito - kenito@300baud.org
23 * @copyright Copyright 2002  MantisBT Team - mantisbt-dev@lists.sourceforge.net
24 * @link http://www.mantisbt.org
25 *
26 * @uses authentication_api.php
27 * @uses constant_inc.php
28 * @uses filter_api.php
29 * @uses gpc_api.php
30 * @uses helper_api.php
31 * @uses user_api.php
32 * @uses user_pref_api.php
33 * @uses utility_api.php
34 */
35
36require_api( 'authentication_api.php' );
37require_api( 'constant_inc.php' );
38require_api( 'filter_api.php' );
39require_api( 'gpc_api.php' );
40require_api( 'helper_api.php' );
41require_api( 'user_api.php' );
42require_api( 'user_pref_api.php' );
43require_api( 'utility_api.php' );
44
45/**
46 * Sets the current user
47 *
48 * @param integer $p_user_id Id to set as current user
49 * @return integer Old current user id
50 * @access public
51 */
52function current_user_set( $p_user_id ) {
53	global $g_cache_current_user_id;
54	global $g_cache_current_user_pref;
55
56	$t_user_id = (int)$p_user_id;
57
58	if( $t_user_id == $g_cache_current_user_id ) {
59		return $t_user_id;
60	}
61
62	$t_old_current = $g_cache_current_user_id;
63	$g_cache_current_user_id = $t_user_id;
64
65	# Clear current user preferences cache
66	$g_cache_current_user_pref = array();
67
68	return $t_old_current;
69}
70
71# ## Current User API ###
72# Wrappers around the User API that pass in the logged-in user for you
73/**
74 * Returns the access level of the current user in the current project
75 *
76 * @return int access level code
77 * @access public
78 */
79function current_user_get_access_level() {
80	return user_get_access_level( auth_get_current_user_id(), helper_get_current_project() );
81}
82
83/**
84 * Returns the number of open issues that are assigned to the current user
85 * in the current project.
86 *
87 * @return int Number of issues assigned to current user that are still open.
88 * @access public
89 */
90function current_user_get_assigned_open_bug_count() {
91	return user_get_assigned_open_bug_count( auth_get_current_user_id(), helper_get_current_project() );
92}
93
94/**
95 * Returns the number of open reported bugs by the current user in
96 * the current project
97 *
98 * @return int Number of issues reported by current user that are still open.
99 * @access public
100 */
101function current_user_get_reported_open_bug_count() {
102	return user_get_reported_open_bug_count( auth_get_current_user_id(), helper_get_current_project() );
103}
104
105/**
106 * Returns the specified field of the currently logged in user
107 *
108 * @param string $p_field_name Name of user property as in the table definition.
109 * @return mixed Get the value of the specified field for current user.
110 * @access public
111 */
112function current_user_get_field( $p_field_name ) {
113	return user_get_field( auth_get_current_user_id(), $p_field_name );
114}
115
116/**
117 * Returns the specified field of the currently logged in user
118 *
119 * @param string $p_pref_name Name of user preference as in the preferences table definition.
120 * @return mixed Get the value of the specified preference for current user.
121 * @access public
122 */
123function current_user_get_pref( $p_pref_name ) {
124	return user_pref_get_pref( auth_get_current_user_id(), $p_pref_name );
125}
126
127/**
128 * Sets the specified preference for the current logged in user.
129 *
130 * @param string                 $p_pref_name  The name of the preference as in the preferences table.
131 * @param boolean|integer|string $p_pref_value The preference new value.
132 * @access public
133 * @return boolean
134 */
135function current_user_set_pref( $p_pref_name, $p_pref_value ) {
136	return user_pref_set_pref( auth_get_current_user_id(), $p_pref_name, $p_pref_value );
137}
138
139/**
140 * Set Current Users Default project in preferences
141 *
142 * @param integer $p_project_id The new default project id.
143 * @return void
144 * @access public
145 */
146function current_user_set_default_project( $p_project_id ) {
147	user_set_default_project( auth_get_current_user_id(), $p_project_id );
148}
149
150/**
151 * Returns an array of projects that are accessible to the current logged in
152 * user.
153 *
154 * @param boolean $p_show_disabled	Include disabled projects.
155 * @return array an array of accessible project ids.
156 * @access public
157 */
158function current_user_get_accessible_projects( $p_show_disabled = false ) {
159	return user_get_accessible_projects( auth_get_current_user_id(), $p_show_disabled );
160}
161
162/**
163 * Returns an array of subprojects of the specified project to which the
164 * currently logged in user has access to.
165 *
166 * @param integer $p_project_id    Parent project id.
167 * @param boolean $p_show_disabled Include disabled projects.
168 * @return array an array of accessible sub-project ids.
169 * @access public
170 */
171function current_user_get_accessible_subprojects( $p_project_id, $p_show_disabled = false ) {
172	return user_get_accessible_subprojects( auth_get_current_user_id(), $p_project_id, $p_show_disabled );
173}
174
175/**
176 * Returns an array of subprojects of the specified project to which the
177 * currently logged in user has access, including subprojects of subprojects
178 *
179 * @param integer $p_project_id Parent project id.
180 * @return array an array of accessible sub-project ids.
181 * @access public
182 */
183function current_user_get_all_accessible_subprojects( $p_project_id ) {
184	return user_get_all_accessible_subprojects( auth_get_current_user_id(), $p_project_id );
185}
186
187/**
188 * Returns true if the currently logged in user is has a role of administrator
189 * or higher, false otherwise
190 *
191 * @return boolean true: administrator; false: otherwise.
192 * @access public
193 */
194function current_user_is_administrator() {
195	return auth_is_user_authenticated() && user_is_administrator( auth_get_current_user_id() );
196}
197
198/**
199 * Returns true if the current user is a protected user, false otherwise.
200 * The $g_anonymous_account user is always considered protected.
201 *
202 * @return true: user is protected; false: otherwise.
203 * @access public
204 */
205function current_user_is_protected() {
206	return user_is_protected( auth_get_current_user_id() );
207}
208
209/**
210 * Returns true if the current user is the anonymous user.
211 *
212 * @return true: user is anonymous; false: otherwise.
213 * @access public
214 */
215function current_user_is_anonymous() {
216	return user_is_anonymous( auth_get_current_user_id() );
217}
218
219/**
220 * Triggers an ERROR if the current user account is protected.
221 * The $g_anonymous_account user is always considered protected.
222 *
223 * @access public
224 * @return void
225 */
226function current_user_ensure_unprotected() {
227	user_ensure_unprotected( auth_get_current_user_id() );
228}
229
230/**
231 * Returns the issue filter for the current user, which is retrieved by
232 * evaluating these steps:
233 * 1) Reads gpc vars for a token id, which means to load a temporary filter
234 * 2) Otherwise, get the filter saved as current, for the user, project
235 *
236 * @param integer $p_project_id Project id to get the user's filter from, if needed.
237 * @return array	A filter array
238 * @access public
239 */
240function current_user_get_bug_filter( $p_project_id = null ) {
241	$f_tmp_key = gpc_get_string( 'filter', null );
242
243	if( null !== $f_tmp_key ) {
244		$t_filter = filter_temporary_get( $f_tmp_key, null );
245		# if filter doesn't exist or can't be loaded, return a default filter (doesn't throw error)
246		if( null === $t_filter ) {
247			$t_filter = filter_get_default();
248		}
249	} else {
250		$t_user_id = auth_get_current_user_id();
251		$t_filter = user_get_bug_filter( $t_user_id, $p_project_id );
252	}
253
254	return $t_filter;
255}
256
257/**
258 * Returns true if the user has access to more that one project
259 *
260 * @return boolean
261 */
262function current_user_has_more_than_one_project() {
263	return user_has_more_than_one_project( auth_get_current_user_id() );
264}
265
266/**
267 * Checks if the user has only one, or none, visible project and modify his
268 * current and default project to be coherent.
269 * - If current project is ALL_PROJECTS, sets the the visible project as current.
270 * - If default project is ALL_PROJECTS, sets the visible project as his default
271 *   project for future sessions.
272 * - If default project is not the visible one, modify it to be that project,
273 *   or ALL_PROJECTS if the user has no accessible projects.
274 *
275 * These changes only apply to users who can't use the project selection.
276 *
277 * @return void
278 */
279function current_user_modify_single_project_default() {
280	# The user must not be able to use the project selector
281	if( layout_navbar_can_show_projects_menu() ) {
282		return;
283	}
284	# The user must have one, or none, projects
285	$t_user_id = auth_get_current_user_id();
286	if( user_has_more_than_one_project( $t_user_id ) ) {
287		return;
288	}
289	$t_default = user_pref_get_pref( $t_user_id, 'default_project' );
290	$t_current = helper_get_current_project();
291	$t_projects = user_get_all_accessible_projects( $t_user_id );
292	$t_count = count( $t_projects );
293
294	if( 0 == $t_count ) {
295		$t_project_id = ALL_PROJECTS;
296	} else {
297		$t_project_id = reset( $t_projects );
298	}
299
300	if( $t_project_id != $t_current ) {
301		helper_set_current_project( $t_project_id );
302	}
303	if( $t_project_id != $t_default ) {
304		user_pref_set_pref( $t_user_id, 'default_project', (int)$t_project_id, ALL_PROJECTS, false /* skip protected check */ );
305	}
306}