Annotation of FreeBSD/tinderbox/webui/module/moduleUsers.php, revision 1.1.1.1.2.2
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.2! as 27: # $Paefchen: FreeBSD/tinderbox/webui/module/moduleUsers.php,v 1.1.1.1.2.1 2007/11/13 07:36:19 as Exp $
1.1 as 28: # $MCom: portstools/tinderbox/webui/module/moduleUsers.php,v 1.18 2007/10/13 02:28:48 ade Exp $
29: #
30:
1.1.1.1.2.2! as 31: class moduleUsers extends moduleSession {
1.1 as 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: if( $this->is_logged_in() ) {
1.1.1.1.2.2! as 43: $user = $this->getAttribute( 'user' );
1.1 as 44: $this->template_assign( 'user_name', $user->getName() );
45: $this->template_assign( 'user_id', $user->getId() );
46: if( $this->checkWwwAdmin() ) {
47: $this->template_assign( 'is_www_admin', true );
48: $this->template_assign( 'all_users', $this->get_all_users() );
49: } else {
50: $this->template_assign( 'is_www_admin', false );
51: }
52: } else {
53: $this->template_assign( 'user_name', '' );
54: }
55:
56: return $this->template_parse( 'display_login.tpl' );
57: }
58:
1.1.1.1.2.1 as 59: public function display_add_user( $user_name, $user_email, $user_password, $www_enabled ) {
1.1 as 60: if( !$this->is_logged_in() ) {
61: return $this->template_parse( 'please_login.tpl' );
62: } elseif ( $this->checkWwwAdmin() ) {
63: $user_properties = $this->display_properties( '', $user_name, $user_email, $user_password, $www_enabled );
64:
65: $this->template_assign( 'user_properties', $user_properties );
66: } else {
67: $this->TinderboxDS->addError( permission_denied );
68: return $this->template_parse( 'user_admin.tpl' );
69: }
70: $this->template_assign( 'add', true );
71: $this->template_assign( 'modify', false );
72: return $this->template_parse( 'user_admin.tpl' );
73: }
74:
1.1.1.1.2.1 as 75: public function display_modify_user( $first, $user_id, $user_name, $user_email, $user_password, $www_enabled ) {
1.1 as 76: if( !$this->is_logged_in() ) {
77: return $this->template_parse( 'please_login.tpl' );
78: }
79:
80: $user = $this->TinderboxDS->getUserById( $user_id );
81: if( $first == 1 ) {
82: $user_name = $user->getName();
83: $user_email = $user->getEmail();
84: $www_enabled = $user->getWwwEnabled();
85: $all_builds = $this->moduleBuilds->get_all_builds();
86: }
87:
88: if( $this->checkWwwAdmin() || ( $this->get_id() == $user->getId() ) ) {
89: $user_properties = $this->display_properties( $user_id, $user_name, $user_email, $user_password, $www_enabled );
90:
91: $this->template_assign( 'user_properties', $user_properties );
92: } else {
93: $this->TinderboxDS->addError( permission_denied );
94: return $this->template_parse( 'user_admin.tpl' );
95: }
96: $this->template_assign( 'add', false );
97: $this->template_assign( 'modify', true );
98: return $this->template_parse( 'user_admin.tpl' );
99: }
100:
1.1.1.1.2.1 as 101: public function display_properties( $user_id, $user_name, $user_email, $user_password, $www_enabled ) {
1.1 as 102: $this->template_assign( 'user_id', $user_id );
103: $this->template_assign( 'user_name', $user_name );
104: $this->template_assign( 'user_email', $user_email );
105: $this->template_assign( 'user_password', $user_password );
106: $this->template_assign( 'www_enabled', $www_enabled );
107: $this->template_assign( 'www_admin', $this->checkWwwAdmin() );
108: return $this->template_parse( 'user_properties.tpl' );
109: }
110:
1.1.1.1.2.1 as 111: public function action_user( $action, $user_id, $user_name, $user_email, $user_password, $www_enabled ) {
1.1 as 112: if( !$this->is_logged_in() ) {
113: return $this->template_parse( 'please_login.tpl' );
114: } elseif( empty( $user_name ) ) {
115: $this->TinderboxDS->addError( user_admin_user_name_empty );
116: return '0';
117: } elseif( $action == 'add' && !$this->checkWwwAdmin() ) {
118: $this->TinderboxDS->addError( permission_denied );
119: return '0';
120: } elseif( $action != 'add' && ( !$this->checkWwwAdmin() && ( $this->get_id() != $user_id ) ) ) {
121: $this->TinderboxDS->addError( permission_denied );
122: return '0';
123: }
124:
125: switch( $action ) {
126: case 'add': $user = new User();
127: $user2 = $this->TinderboxDS->getUserByName( $user_name );
128: if( is_object( $user2 ) && $user2->getId() ) {
129: $this->TinderboxDS->addError( user_admin_user_exists." (".$user_name.")" );
130: return '0';
131: }
132: unset( $user2 );
133: break;
134: case 'modify': $user = $this->TinderboxDS->getUserById( $user_id );
135: if( !is_object( $user ) || !$user->getId() ) {
136: $this->TinderboxDS->addError( user_admin_user_not_exist );
137: return '0';
138: }
139: if( $user_name != $user->getName() ) {
140: if( !$this->checkWwwAdmin() ) {
141: $this->TinderboxDS->addError( user_admin_user_name_changed );
142: return '0';
143: } else {
144: $user2 = $this->TinderboxDS->getUserByName( $user_name );
145: if( is_object( $user2 ) && $user2->getId() ) {
146: $this->TinderboxDS->addError( user_admin_user_exists." (".$user_name.")" );
147: return '0';
148: }
149: unset( $user2 );
150: }
151: }
152: break;
153: case 'delete': $user = $this->TinderboxDS->getUserById( $user_id );
154: if( !is_object( $user ) || !$user->getId() ) {
155: $this->TinderboxDS->addError( user_admin_user_not_exist );
156: return '0';
157: }
158: break;
159: default: return '0';
160: break;
161: }
162:
163: switch( $www_enabled ) {
164: case '1': $www_enabled = 1; break;
165: default: $www_enabled = 0; break;
166: }
167:
168: $user->setName( $user_name );
169: $user->setEmail( $user_email );
170: $user->setWwwEnabled( $www_enabled );
171: if( $user_password ) {
172: $user->setPassword( cryptPassword( $user_password ) );
173: }
174:
175: $this->TinderboxDS->start_transaction();
176:
177: switch( $action ) {
178: case 'add': if( !$this->TinderboxDS->addUser( $user ) ) {
179: $this->TinderboxDS->rollback_transaction();
180: return '0';
181: }
182: $user = $this->TinderboxDS->getUserByName( $user_name );
183: break;
184: case 'modify': if( !$this->TinderboxDS->updateUser( $user ) ) {
185: $this->TinderboxDS->rollback_transaction();
186: return '0';
187: }
188: if( $this->checkWwwAdmin() && !$this->TinderboxDS->deleteUserPermissions( $user, 'builds' ) ) {
189: $this->TinderboxDS->rollback_transaction();
190: return '0';
191: }
192: break;
193: case 'delete': if( !$this->TinderboxDS->deleteUser( $user ) ) {
194: $this->TinderboxDS->rollback_transaction();
195: return '0';
196: } else {
197: $this->TinderboxDS->commit_transaction();
198: return '1';
199: }
200: break;
201: }
202:
203: $this->TinderboxDS->commit_transaction();
204: return '1';
205: }
206:
1.1.1.1.2.1 as 207: public function do_login( $username, $password ) {
1.1 as 208: $user = $this->TinderboxDS->getUserByLogin( $username, $password );
209: if( $user ) {
210: if( $user->getWwwEnabled() ) {
1.1.1.1.2.2! as 211: $this->setAttribute( 'user', $user );
1.1 as 212: return true;
213: } else {
214: $this->TinderboxDS->addError( user_login_not_enabled );
215: }
216: } else {
217: $this->TinderboxDS->addError( user_login_wrong_data );
218: }
219:
220: return false;
221: }
222:
1.1.1.1.2.1 as 223: public function do_logout() {
1.1.1.1.2.2! as 224: $this->removeAttribute( 'user' );
! 225: $this->destroy();
1.1 as 226:
227: return true;
228: }
229:
1.1.1.1.2.1 as 230: public function is_logged_in() {
1.1.1.1.2.2! as 231: $user = $this->getAttribute( 'user' );
1.1 as 232:
233: if( is_object( $user ) && $user->getWwwEnabled() == 1 ) {
234: return true;
235: }
236: return false;
237: }
238:
1.1.1.1.2.1 as 239: public function get_www_enabled() {
1.1.1.1.2.2! as 240: $user = $this->getAttribute( 'user' );
1.1 as 241:
242: $userobj = $this->TinderboxDS->getUserById( $user->getId() );
243: if( is_object( $userobj ) ) {
244: return $userobj->getWwwEnabled();
245: }
246:
247: return false;
248: }
249:
1.1.1.1.2.1 as 250: public function get_id() {
1.1.1.1.2.2! as 251: $user = $this->getAttribute( 'user' );
1.1 as 252:
253: return $user->getId();
254: }
255:
1.1.1.1.2.1 as 256: public function get_all_users() {
1.1 as 257: $all_users_raw = $this->TinderboxDS->getAllUsers();
258: $all_users = array();
259: foreach( $all_users_raw as $user ) {
260: $all_users[] = array( 'user_id' => $user->getId(), 'user_name' => $user->getName() );
261: }
262: return $all_users;
263: }
264:
1.1.1.1.2.1 as 265: public function fetch_permissions( $object_type, $object_id ) {
1.1 as 266:
267: if( $this->is_logged_in() ) {
1.1.1.1.2.2! as 268: $user = $this->getAttribute( 'user' );
1.1 as 269: foreach( $this->TinderboxDS->getUserPermissions( $user->getId(), $object_type, $object_id ) as $perm ) {
270: $this->permissions[$object_type][$object_id][$perm['user_permission']] = 1;
271: }
272: $this->permissions[$object_type][$object_id]['set'] = 1;
273: return true;
274: } else {
275: return false;
276: }
277: }
278:
1.1.1.1.2.1 as 279: public function get_permission( $object_type, $object_id, $permission ) {
1.1 as 280: if( !is_array( $this->permissions[$object_type][$object_id] ) && !isset( $this->permissions[$object_type][$object_id]['set'] ) ) {
281: $this->fetch_permissions( $object_type, $object_id );
282: }
283: if( isset( $this->permissions[$object_type][$object_id][$permission] ) ) {
284: return true;
285: } else {
286: return false;
287: }
288: }
289:
1.1.1.1.2.1 as 290: public function checkWwwAdmin() {
1.1 as 291: return $this->get_permission( 'users', $this->get_id(), 'IS_WWW_ADMIN' );
292: }
293: }
294:
1.1.1.1.2.1 as 295: /* old?
1.1 as 296: function checkPermAddQueue( $object_type, $object_id ) {
297: return $this->get_permission( $object_type, $object_id, 'PERM_ADD_QUEUE' );
298: }
299:
300: function checkPermModifyOwnQueue( $object_type, $object_id ) {
301: return $this->get_permission( $object_type, $object_id, 'PERM_MODIFY_OWN_QUEUE' );
302: }
303:
304: function checkPermDeleteOwnQueue( $object_type, $object_id ) {
305: return $this->get_permission( $object_type, $object_id, 'PERM_DELETE_OWN_QUEUE' );
306: }
307:
308: function checkPermPrioLower5( $object_type, $object_id ) {
309: return $this->get_permission( $object_type, $object_id, 'PERM_PRIO_LOWER_5' );
310: }
311:
312: function checkPermModifyOtherQueue( $object_type, $object_id )
313: {
314: return $this->get_permission( $object_type, $object_id, 'PERM_MODIFY_OTHER_QUEUE' );
315: }
316:
317: function checkPermDeleteOtherQueue( $object_type, $object_id )
318: {
319: return $this->get_permission( $object_type, $object_id, 'PERM_DELETE_OTHER_QUEUE' );
320: }
1.1.1.1.2.1 as 321: */
1.1 as 322:
323: ?>