Phundament App Class Reference
  • Package
  • Class
  • Tree

Packages

  • bootstrap
    • widgets
      • input
  • Image
  • None
  • p3admin
  • p3extensions
    • behaviors
    • commands
    • components
    • helpers
    • widgets
  • p3media
    • actions
    • controllers
    • models
  • p3pages
    • models
  • p3widgets
    • components
    • models
  • PHP
  • system
    • db
      • ar
    • gii
    • web
      • auth
      • helpers
      • widgets
  • yiiext
    • widgets
      • fancybox
      • lipsum
  • zii
    • widgets
      • grid

Classes

  • ActivationController
  • AdminController
  • AssignmentController
  • AssignmentForm
  • AuthChildForm
  • AuthItemController
  • AuthItemForm
  • BaseP3Widget
  • BaseP3WidgetMeta
  • BaseP3WidgetTranslation
  • Bootstrap
  • CImageComponent
  • CKEditor
  • CodeProvider
  • DefaultController
  • EChosen
  • EDbMigration
  • EditableField
  • EditableSaver
  • EMigrateCommand
  • ESelect2
  • EUserFlash
  • FullCrudFieldProvider
  • GenerateForm
  • IdentificationColumnValidator
  • InstallController
  • JSONEditorView
  • JuiJSONEditorInput
  • LoginController
  • LogoutController
  • Metadata
  • P3CrudFieldProvider
  • P3MediaController
  • P3MediaMetaController
  • P3MediaSelect
  • P3PageController
  • P3PageMetaController
  • P3PageTranslationController
  • P3Widget
  • P3WidgetController
  • P3WidgetMetaController
  • P3WidgetTranslation
  • P3WidgetTranslationController
  • ProfileController
  • ProfileFieldController
  • RAuthItemBehavior
  • RAuthItemChildDataProvider
  • RAuthItemDataProvider
  • RAuthItemParentDataProvider
  • RAuthorizer
  • RController
  • RDbAuthManager
  • RecoveryController
  • RegistrationController
  • RegistrationForm
  • RGenerator
  • Rights
  • RightsFilter
  • RightsModule
  • RInstaller
  • RPermissionDataProvider
  • RUserBehavior
  • RWebUser
  • UActiveForm
  • UploadHandler
  • UserChangePassword
  • UserController
  • UserIdentity
  • UserLogin
  • UserModule
  • UserRecoveryForm
  • UWdropDownDep
  • UWfile
  • UWjuiAutoComplete
  • UWjuidate
  • UWrelBelongsTo
  • WebUserBehavior
  1 <?php
  2 /**
  3 * Rights installer component class file.
  4 *
  5 * @author Christoffer Niska <cniska@live.com>
  6 * @copyright Copyright &copy; 2010 Christoffer Niska
  7 * @since 0.9.3
  8 */
  9 class RInstaller extends CApplicationComponent
 10 {
 11     const ERROR_NONE=0;
 12     const ERROR_QUERY_FAILED=1;
 13 
 14     /**
 15     * @property array the roles assigned to users implicitly.
 16     */
 17     public $defaultRoles;
 18     /**
 19     * @property string the name of the superuser role.
 20     */
 21     public $superuserName;
 22     /**
 23     * @property string the name of the authenticated role.
 24     */
 25     public $authenticatedName;
 26     /**
 27     * @property string the name of the guest role.
 28     */
 29     public $guestName;
 30     /**
 31      * @property RAuthManager the authorization manager.
 32      */
 33     private $_authManager;
 34     /**
 35      * @property boolean whether Rights is installed.
 36      */
 37     private $_installed;
 38 
 39     /**
 40     * @property CDbConnection
 41     */
 42     public $db;
 43 
 44     /**
 45     * Initializes the installer.
 46     * @throws CException if the authorization manager or the web user
 47     * is not configured to use the correct class.
 48     */
 49     public function init()
 50     {
 51         parent::init();
 52 
 53         // Make sure the application is configured
 54         // to use a valid authorization manager.
 55         $authManager = Yii::app()->getAuthManager();
 56         if( ($authManager instanceof RDbAuthManager)===false )
 57             throw new CException(Rights::t('install', 'Application authorization manager must extend the RDbAuthManager class.'));
 58 
 59         // Make sure the application is configured
 60         // to use a valid web user.
 61         $user = Yii::app()->getUser();
 62         if( ($user instanceof RWebUser)===false )
 63             throw new CException(Rights::t('install', 'Application web user must extend the RWebUser class.'));
 64 
 65         $this->_authManager = $authManager;
 66         $this->db = $this->_authManager->db;
 67     }
 68 
 69     /**
 70     * Runs the installer.
 71     * @param boolean whether to drop tables if they exists.
 72     * @return boolean whether the installer ran successfully.
 73     */
 74     public function run()
 75     {
 76         // Get the table names.
 77         $itemTable = $this->_authManager->itemTable;
 78         $itemChildTable = $this->_authManager->itemChildTable;
 79         $assignmentTable = $this->_authManager->assignmentTable;
 80         $rightsTable = $this->_authManager->rightsTable;
 81 
 82         // Fetch the schema.
 83         $schema = file_get_contents(dirname(__FILE__).'/../data/schema.sql');
 84 
 85         // Correct the table names.
 86         $schema = strtr($schema, array(
 87             'AuthItem'=>$itemTable,
 88             'AuthItemChild'=>$itemChildTable,
 89             'AuthAssignment'=>$assignmentTable,
 90             'Rights'=>$rightsTable,
 91         ));
 92 
 93         // Convert the schema into an array of sql queries.
 94         $schema = preg_split("/;\s*/", trim($schema, ';'));
 95 
 96         // Start transaction
 97         $txn = $this->db->beginTransaction();
 98 
 99         try
100         {
101             // Execute each query in the schema.
102             foreach( $schema as $sql )
103             {
104                 $command = $this->db->createCommand($sql);
105                 $command->execute();
106             }
107 
108             // Insert the necessary roles.
109             $roles = $this->getUniqueRoles();
110             foreach( $roles as $roleName )
111             {
112                 $sql = "INSERT INTO {$itemTable} (name, type, data)
113                     VALUES (:name, :type, :data)";
114                 $command = $this->db->createCommand($sql);
115                 $command->bindValue(':name', $roleName);
116                 $command->bindValue(':type', CAuthItem::TYPE_ROLE);
117                 $command->bindValue(':data', 'N;');
118                 $command->execute();
119             }
120 
121             // Assign the logged in user the superusers role.
122             $sql = "INSERT INTO {$assignmentTable} (itemname, userid, data)
123                 VALUES (:itemname, :userid, :data)";
124             $command = $this->db->createCommand($sql);
125             $command->bindValue(':itemname', $this->superuserName);
126             $command->bindValue(':userid', Yii::app()->getUser()->id);
127             $command->bindValue(':data', 'N;');
128             $command->execute();
129 
130             // All commands executed successfully, commit.
131             $txn->commit();
132             return self::ERROR_NONE;
133         }
134         catch( CDbException $e )
135         {
136             // Something went wrong, rollback.
137             $txn->rollback();
138 
139             return self::ERROR_QUERY_FAILED;
140         }
141     }
142 
143     /**
144     * Returns a list of unique roles names.
145     * @return array the list of roles.
146     */
147     private function getUniqueRoles()
148     {
149         $roles = CMap::mergeArray($this->defaultRoles, array(
150             $this->superuserName,
151             $this->authenticatedName,
152             $this->guestName,
153         ));
154         return array_unique($roles);
155     }
156 
157     /**
158     * @return boolean whether Rights is installed.
159     */
160     public function getInstalled()
161     {
162         if( $this->_installed!==null )
163         {
164             return $this->_installed;
165         }
166         else
167         {
168             $schema = array(
169                 "SELECT COUNT(*) FROM {$this->_authManager->itemTable}",
170                 "SELECT COUNT(*) FROM {$this->_authManager->itemChildTable}",
171                 "SELECT COUNT(*) FROM {$this->_authManager->assignmentTable}",
172                 "SELECT COUNT(*) FROM {$this->_authManager->rightsTable}",
173             );
174 
175             try
176             {
177                 foreach( $schema as $sql )
178                 {
179                     $command = $this->db->createCommand($sql);
180                     $command->queryScalar();
181                 }
182 
183                 $installed = true;
184             }
185             catch( CDbException $e )
186             {
187                 $installed = false;
188             }
189 
190             return $this->_installed = $installed;
191         }
192     }
193 }
194 
Phundament App Class Reference API documentation generated by ApiGen 2.8.0