Annotation of FreeBSD/tinderbox/webui/module/moduleUsers.php, revision 1.1.1.1.2.1
1.1 as 1: <?php
2: #-
3: # Copyright (c) 2005 Oliver Lehmann <oliver@FreeBSD.org>
4: # All rights reserved.
5: #
6: # Redistribution and use in source and binary forms, with or without
7: # modification, are permitted provided that the following conditions
8: # are met:
9: # 1. Redistributions of source code must retain the above copyright
10: # notice, this list of conditions and the following disclaimer
11: # 2. Redistributions in binary form must reproduce the above copyright
12: # notice, this list of conditions and the following disclaimer in the
13: # documentation and/or other materials provided with the distribution.
14: #
15: # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16: # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17: # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18: # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19: # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20: # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21: # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22: # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23: # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24: # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25: # SUCH DAMAGE.
26: #
1.1.1.1.2.1! as 27: # $Paefchen$
1.1 as 28: # $MCom: portstools/tinderbox/webui/module/moduleUsers.php,v 1.18 2007/10/13 02:28:48 ade Exp $
29: #
30:
31: class moduleUsers extends module {
32:
1.1.1.1.2.1! as 33: private $permissions = null;
! 34: private $moduleBuilds = null;
1.1 as 35:
1.1.1.1.2.1! as 36: public function __construct() {
! 37: parent::__construct();
! 38: $this->moduleBuilds = new moduleBuilds;
1.1 as 39: }
40:
1.1.1.1.2.1! as 41: public function display_login() {
1.1 as 42: global $moduleSession;
43:
44: if( $this->is_logged_in() ) {
45: $user = $moduleSession->getAttribute( 'user' );
46: $this->template_assign( 'user_name', $user->getName() );
47: $this->template_assign( 'user_id', $user->getId() );
48: if( $this->checkWwwAdmin() ) {
49: $this->template_assign( 'is_www_admin', true );
50: $this->template_assign( 'all_users', $this->get_all_users() );
51: } else {
52: $this->template_assign( 'is_www_admin', false );
53: }
54: } else {
55: $this->template_assign( 'user_name', '' );
56: }
57:
58: return $this->template_parse( 'display_login.tpl' );
59: }
60:
1.1.1.1.2.1! as 61: public function display_add_user( $user_name, $user_email, $user_password, $www_enabled ) {
1.1 as 62: if( !$this->is_logged_in() ) {
63: return $this->template_parse( 'please_login.tpl' );
64: } elseif ( $this->checkWwwAdmin() ) {
65: $user_properties = $this->display_properties( '', $user_name, $user_email, $user_password, $www_enabled );
66:
67: $this->template_assign( 'user_properties', $user_properties );
68: } else {
69: $this->TinderboxDS->addError( permission_denied );
70: return $this->template_parse( 'user_admin.tpl' );
71: }
72: $this->template_assign( 'add', true );
73: $this->template_assign( 'modify', false );
74: return $this->template_parse( 'user_admin.tpl' );
75: }
76:
1.1.1.1.2.1! as 77: public function display_modify_user( $first, $user_id, $user_name, $user_email, $user_password, $www_enabled ) {
1.1 as 78: if( !$this->is_logged_in() ) {
79: return $this->template_parse( 'please_login.tpl' );
80: }
81:
82: $user = $this->TinderboxDS->getUserById( $user_id );
83: if( $first == 1 ) {
84: $user_name = $user->getName();
85: $user_email = $user->getEmail();
86: $www_enabled = $user->getWwwEnabled();
87: $all_builds = $this->moduleBuilds->get_all_builds();
88: }
89:
90: if( $this->checkWwwAdmin() || ( $this->get_id() == $user->getId() ) ) {
91: $user_properties = $this->display_properties( $user_id, $user_name, $user_email, $user_password, $www_enabled );
92:
93: $this->template_assign( 'user_properties', $user_properties );
94: } else {
95: $this->TinderboxDS->addError( permission_denied );
96: return $this->template_parse( 'user_admin.tpl' );
97: }
98: $this->template_assign( 'add', false );
99: $this->template_assign( 'modify', true );
100: return $this->template_parse( 'user_admin.tpl' );
101: }
102:
1.1.1.1.2.1! as 103: public function display_properties( $user_id, $user_name, $user_email, $user_password, $www_enabled ) {
1.1 as 104: $this->template_assign( 'user_id', $user_id );
105: $this->template_assign( 'user_name', $user_name );
106: $this->template_assign( 'user_email', $user_email );
107: $this->template_assign( 'user_password', $user_password );
108: $this->template_assign( 'www_enabled', $www_enabled );
109: $this->template_assign( 'www_admin', $this->checkWwwAdmin() );
110: return $this->template_parse( 'user_properties.tpl' );
111: }
112:
1.1.1.1.2.1! as 113: public function action_user( $action, $user_id, $user_name, $user_email, $user_password, $www_enabled ) {
1.1 as 114: if( !$this->is_logged_in() ) {
115: return $this->template_parse( 'please_login.tpl' );
116: } elseif( empty( $user_name ) ) {
117: $this->TinderboxDS->addError( user_admin_user_name_empty );
118: return '0';
119: } elseif( $action == 'add' && !$this->checkWwwAdmin() ) {
120: $this->TinderboxDS->addError( permission_denied );
121: return '0';
122: } elseif( $action != 'add' && ( !$this->checkWwwAdmin() && ( $this->get_id() != $user_id ) ) ) {
123: $this->TinderboxDS->addError( permission_denied );
124: return '0';
125: }
126:
127: switch( $action ) {
128: case 'add': $user = new User();
129: $user2 = $this->TinderboxDS->getUserByName( $user_name );
130: if( is_object( $user2 ) && $user2->getId() ) {
131: $this->TinderboxDS->addError( user_admin_user_exists." (".$user_name.")" );
132: return '0';
133: }
134: unset( $user2 );
135: break;
136: case 'modify': $user = $this->TinderboxDS->getUserById( $user_id );
137: if( !is_object( $user ) || !$user->getId() ) {
138: $this->TinderboxDS->addError( user_admin_user_not_exist );
139: return '0';
140: }
141: if( $user_name != $user->getName() ) {
142: if( !$this->checkWwwAdmin() ) {
143: $this->TinderboxDS->addError( user_admin_user_name_changed );
144: return '0';
145: } else {
146: $user2 = $this->TinderboxDS->getUserByName( $user_name );
147: if( is_object( $user2 ) && $user2->getId() ) {
148: $this->TinderboxDS->addError( user_admin_user_exists." (".$user_name.")" );
149: return '0';
150: }
151: unset( $user2 );
152: }
153: }
154: break;
155: case 'delete': $user = $this->TinderboxDS->getUserById( $user_id );
156: if( !is_object( $user ) || !$user->getId() ) {
157: $this->TinderboxDS->addError( user_admin_user_not_exist );
158: return '0';
159: }
160: break;
161: default: return '0';
162: break;
163: }
164:
165: switch( $www_enabled ) {
166: case '1': $www_enabled = 1; break;
167: default: $www_enabled = 0; break;
168: }
169:
170: $user->setName( $user_name );
171: $user->setEmail( $user_email );
172: $user->setWwwEnabled( $www_enabled );
173: if( $user_password ) {
174: $user->setPassword( cryptPassword( $user_password ) );
175: }
176:
177: $this->TinderboxDS->start_transaction();
178:
179: switch( $action ) {
180: case 'add': if( !$this->TinderboxDS->addUser( $user ) ) {
181: $this->TinderboxDS->rollback_transaction();
182: return '0';
183: }
184: $user = $this->TinderboxDS->getUserByName( $user_name );
185: break;
186: case 'modify': if( !$this->TinderboxDS->updateUser( $user ) ) {
187: $this->TinderboxDS->rollback_transaction();
188: return '0';
189: }
190: if( $this->checkWwwAdmin() && !$this->TinderboxDS->deleteUserPermissions( $user, 'builds' ) ) {
191: $this->TinderboxDS->rollback_transaction();
192: return '0';
193: }
194: break;
195: case 'delete': if( !$this->TinderboxDS->deleteUser( $user ) ) {
196: $this->TinderboxDS->rollback_transaction();
197: return '0';
198: } else {
199: $this->TinderboxDS->commit_transaction();
200: return '1';
201: }
202: break;
203: }
204:
205: $this->TinderboxDS->commit_transaction();
206: return '1';
207: }
208:
1.1.1.1.2.1! as 209: public function do_login( $username, $password ) {
1.1 as 210: global $moduleSession;
211:
212: $user = $this->TinderboxDS->getUserByLogin( $username, $password );
213: if( $user ) {
214: if( $user->getWwwEnabled() ) {
215: $moduleSession->setAttribute( 'user', $user );
216: return true;
217: } else {
218: $this->TinderboxDS->addError( user_login_not_enabled );
219: }
220: } else {
221: $this->TinderboxDS->addError( user_login_wrong_data );
222: }
223:
224: return false;
225: }
226:
1.1.1.1.2.1! as 227: public function do_logout() {
1.1 as 228: global $moduleSession;
229:
230: $moduleSession->removeAttribute( 'user' );
231: $moduleSession->destroy();
232:
233: return true;
234: }
235:
1.1.1.1.2.1! as 236: public function is_logged_in() {
1.1 as 237: global $moduleSession;
238:
239: $user = $moduleSession->getAttribute( 'user' );
240:
241: if( is_object( $user ) && $user->getWwwEnabled() == 1 ) {
242: return true;
243: }
244: return false;
245: }
246:
1.1.1.1.2.1! as 247: public function get_www_enabled() {
1.1 as 248: global $moduleSession;
249: $user = $moduleSession->getAttribute( 'user' );
250:
251: $userobj = $this->TinderboxDS->getUserById( $user->getId() );
252: if( is_object( $userobj ) ) {
253: return $userobj->getWwwEnabled();
254: }
255:
256: return false;
257: }
258:
1.1.1.1.2.1! as 259: public function get_id() {
1.1 as 260: global $moduleSession;
261: $user = $moduleSession->getAttribute( 'user' );
262:
263: return $user->getId();
264: }
265:
1.1.1.1.2.1! as 266: public function get_all_users() {
1.1 as 267: $all_users_raw = $this->TinderboxDS->getAllUsers();
268: $all_users = array();
269: foreach( $all_users_raw as $user ) {
270: $all_users[] = array( 'user_id' => $user->getId(), 'user_name' => $user->getName() );
271: }
272: return $all_users;
273: }
274:
1.1.1.1.2.1! as 275: public function fetch_permissions( $object_type, $object_id ) {
1.1 as 276: global $moduleSession;
277:
278: if( $this->is_logged_in() ) {
279: $user = $moduleSession->getAttribute( 'user' );
280: foreach( $this->TinderboxDS->getUserPermissions( $user->getId(), $object_type, $object_id ) as $perm ) {
281: $this->permissions[$object_type][$object_id][$perm['user_permission']] = 1;
282: }
283: $this->permissions[$object_type][$object_id]['set'] = 1;
284: return true;
285: } else {
286: return false;
287: }
288: }
289:
1.1.1.1.2.1! as 290: public function get_permission( $object_type, $object_id, $permission ) {
1.1 as 291: if( !is_array( $this->permissions[$object_type][$object_id] ) && !isset( $this->permissions[$object_type][$object_id]['set'] ) ) {
292: $this->fetch_permissions( $object_type, $object_id );
293: }
294: if( isset( $this->permissions[$object_type][$object_id][$permission] ) ) {
295: return true;
296: } else {
297: return false;
298: }
299: }
300:
1.1.1.1.2.1! as 301: public function checkWwwAdmin() {
1.1 as 302: return $this->get_permission( 'users', $this->get_id(), 'IS_WWW_ADMIN' );
303: }
304: }
305:
1.1.1.1.2.1! as 306: /* old?
1.1 as 307: function checkPermAddQueue( $object_type, $object_id ) {
308: return $this->get_permission( $object_type, $object_id, 'PERM_ADD_QUEUE' );
309: }
310:
311: function checkPermModifyOwnQueue( $object_type, $object_id ) {
312: return $this->get_permission( $object_type, $object_id, 'PERM_MODIFY_OWN_QUEUE' );
313: }
314:
315: function checkPermDeleteOwnQueue( $object_type, $object_id ) {
316: return $this->get_permission( $object_type, $object_id, 'PERM_DELETE_OWN_QUEUE' );
317: }
318:
319: function checkPermPrioLower5( $object_type, $object_id ) {
320: return $this->get_permission( $object_type, $object_id, 'PERM_PRIO_LOWER_5' );
321: }
322:
323: function checkPermModifyOtherQueue( $object_type, $object_id )
324: {
325: return $this->get_permission( $object_type, $object_id, 'PERM_MODIFY_OTHER_QUEUE' );
326: }
327:
328: function checkPermDeleteOtherQueue( $object_type, $object_id )
329: {
330: return $this->get_permission( $object_type, $object_id, 'PERM_DELETE_OTHER_QUEUE' );
331: }
1.1.1.1.2.1! as 332: */
1.1 as 333:
334: ?>