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