library/Garp/Cli/Command/Admin.php
public function add(array $args = array()) {
        $ini = Garp_Auth::getInstance()->getConfigValues();
        if (empty($ini['adapters']['db'])) {
            Garp_Cli::errorOut('Error: DB adapter is not configured in application.ini.');
        } elseif (empty($ini['adapters']['db']['identityColumn'])
            || empty($ini['adapters']['db']['credentialColumn'])
        ) {
            Garp_Cli::errorOut(
                'Error: identityColumn or credentialColumn not configured in application.ini'
            );
        } else {
            $newUserData = array(
                'role' => 'admin'
            );
            $promptData = array();

            // Pull required fields from Spawner config
            $modelSet = Garp_Spawn_Model_Set::getInstance();
            $userModelConfig = $modelSet['User'];
            $requiredFields = $userModelConfig->fields->getFields('required', true);
            foreach ($requiredFields as $field) {
                if ($field->origin == 'config' && $field->name !== 'id') {
                    $promptData[] = $field->name;
                } elseif ($field->origin == 'relation') {
                    Garp_Cli::errorOut(
                        'Field ' . $field->name .
                        ' is required but must be filled by way of relation. ' .
                        'This makes it impossible to create an admin from the commandline.'
                    );
                }
            }

            if (!in_array($ini['adapters']['db']['identityColumn'], $promptData)) {
                $promptData[] = $ini['adapters']['db']['identityColumn'];
            }

            // prompt for the new data
            Garp_Cli::lineOut('Please fill the following columns:');
            foreach ($promptData as $key) {
                $newUserData[$key] = trim(Garp_Cli::prompt($key . ':'));
            }

            $newAuthLocalData = array(
                'password' => trim(Garp_Cli::prompt('Choose a password:'))
            );

            /**
             * A lot of assumptions are made here;
             * - a users table is available, as well as a User model
             * - an auth_local table is available, following the conventions set by Garp
             * - the users table has an id, name, email and role column, while the password
             *   column resides in the auth_local table
             *
             * While all this is the preferred method, it's entirely possible to circumvent these
             * conventions and come up with project-specific standards.
             * In that case however, this CLI command is not for you.
             */
            $user = new Model_User();
            try {
                $id = $user->insert($newUserData);
                $authLocal = new Model_AuthLocal();
                $newAuthLocalData['user_id'] = $id;
                if ($authLocal->insert($newAuthLocalData)) {
                    Garp_Cli::lineOut('Successfully created the administrator. (id: ' . $id . ')');
                } else {
                    Garp_Cli::errorOut('Error: could not create administrator.');
                }
            } catch (Zend_Db_Statement_Exception $e) {
                if (strpos($e->getMessage(), 'Duplicate entry') !== false
                    && strpos($e->getMessage(), 'email_unique') !== false
                ) {
                    Garp_Cli::errorOut(
                        'Error: this email address is already in use. ' .
                        'Maybe you meant to use Garp Admin make?'
                    );
                } else {
                    throw $e;
                }
            }
        }
    }
public function add(array $args = array()) {
        $ini = Garp_Auth::getInstance()->getConfigValues();
        if (empty($ini['adapters']['db'])) {
            Garp_Cli::errorOut('Error: DB adapter is not configured in application.ini.');
        } elseif (empty($ini['adapters']['db']['identityColumn'])
            || empty($ini['adapters']['db']['credentialColumn'])
        ) {
            Garp_Cli::errorOut(
                'Error: identityColumn or credentialColumn not configured in application.ini'
            );
        } else {
            $newUserData = array(
                'role' => 'admin'
            );
            $promptData = array();

            // Pull required fields from Spawner config
            $modelSet = Garp_Spawn_Model_Set::getInstance();
            $userModelConfig = $modelSet['User'];
            $requiredFields = $userModelConfig->fields->getFields('required', true);
            foreach ($requiredFields as $field) {
                if ($field->origin == 'config' && $field->name !== 'id') {
                    $promptData[] = $field->name;
                } elseif ($field->origin == 'relation') {
                    Garp_Cli::errorOut(
                        'Field ' . $field->name .
                        ' is required but must be filled by way of relation. ' .
                        'This makes it impossible to create an admin from the commandline.'
                    );
                }
            }

            if (!in_array($ini['adapters']['db']['identityColumn'], $promptData)) {
                $promptData[] = $ini['adapters']['db']['identityColumn'];
            }

            // prompt for the new data
            Garp_Cli::lineOut('Please fill the following columns:');
            foreach ($promptData as $key) {
                $newUserData[$key] = trim(Garp_Cli::prompt($key . ':'));
            }

            $newAuthLocalData = array(
                'password' => trim(Garp_Cli::prompt('Choose a password:'))
            );

            /**
             * A lot of assumptions are made here;
             * - a users table is available, as well as a User model
             * - an auth_local table is available, following the conventions set by Garp
             * - the users table has an id, name, email and role column, while the password
             *   column resides in the auth_local table
             *
             * While all this is the preferred method, it's entirely possible to circumvent these
             * conventions and come up with project-specific standards.
             * In that case however, this CLI command is not for you.
             */
            $user = new Model_User();
            try {
                $id = $user->insert($newUserData);
                $authLocal = new Model_AuthLocal();
                $newAuthLocalData['user_id'] = $id;
                if ($authLocal->insert($newAuthLocalData)) {
                    Garp_Cli::lineOut('Successfully created the administrator. (id: ' . $id . ')');
                } else {
                    Garp_Cli::errorOut('Error: could not create administrator.');
                }
            } catch (Zend_Db_Statement_Exception $e) {
                if (strpos($e->getMessage(), 'Duplicate entry') !== false
                    && strpos($e->getMessage(), 'email_unique') !== false
                ) {
                    Garp_Cli::errorOut(
                        'Error: this email address is already in use. ' .
                        'Maybe you meant to use Garp Admin make?'
                    );
                } else {
                    throw $e;
                }
            }
        }
    }
public function add ( array $ args = array ( ) ) { $ ini = Garp_Auth :: getInstance ( ) -> getConfigValues ( ) ; if ( empty ( $ ini [ 'adapters' ] [ 'db' ] ) ) { Garp_Cli :: errorOut ( 'Error: DB adapter is not configured in application.ini.' ) ; } elseif ( empty ( $ ini [ 'adapters' ] [ 'db' ] [ 'identityColumn' ] ) || empty ( $ ini [ 'adapters' ] [ 'db' ] [ 'credentialColumn' ] ) ) { Garp_Cli :: errorOut ( 'Error: identityColumn or credentialColumn not configured in application.ini' ) ; } else { $ newUserData = array ( 'role' => 'admin' ) ; $ promptData = array ( ) ; // Pull required fields from Spawner config $ modelSet = Garp_Spawn_Model_Set :: getInstance ( ) ; $ userModelConfig = $ modelSet [ 'User' ] ; $ requiredFields = $ userModelConfig -> fields -> getFields ( 'required' , true ) ; foreach ( $ requiredFields as $ field ) { if ( $ field -> origin == 'config' && $ field -> name !== 'id' ) { $ promptData [ ] = $ field -> name ; } elseif ( $ field -> origin == 'relation' ) { Garp_Cli :: errorOut ( 'Field ' . $ field -> name . ' is required but must be filled by way of relation. ' . 'This makes it impossible to create an admin from the commandline.' ) ; } } if ( ! in_array ( $ ini [ 'adapters' ] [ 'db' ] [ 'identityColumn' ] , $ promptData ) ) { $ promptData [ ] = $ ini [ 'adapters' ] [ 'db' ] [ 'identityColumn' ] ; } // prompt for the new data Garp_Cli :: lineOut ( 'Please fill the following columns:' ) ; foreach ( $ promptData as $ key ) { $ newUserData [ $ key ] = trim ( Garp_Cli :: prompt ( $ key . ':' ) ) ; } $ newAuthLocalData = array ( 'password' => trim ( Garp_Cli :: prompt ( 'Choose a password:' ) ) ) ; /**
             * A lot of assumptions are made here;
             * - a users table is available, as well as a User model
             * - an auth_local table is available, following the conventions set by Garp
             * - the users table has an id, name, email and role column, while the password
             *   column resides in the auth_local table
             *
             * While all this is the preferred method, it's entirely possible to circumvent these
             * conventions and come up with project-specific standards.
             * In that case however, this CLI command is not for you.
             */ $ user = new Model_User ( ) ; try { $ id = $ user -> insert ( $ newUserData ) ; $ authLocal = new Model_AuthLocal ( ) ; $ newAuthLocalData [ 'user_id' ] = $ id ; if ( $ authLocal -> insert ( $ newAuthLocalData ) ) { Garp_Cli :: lineOut ( 'Successfully created the administrator. (id: ' . $ id . ')' ) ; } else { Garp_Cli :: errorOut ( 'Error: could not create administrator.' ) ; } } catch ( Zend_Db_Statement_Exception $ e ) { if ( strpos ( $ e -> getMessage ( ) , 'Duplicate entry' ) !== false && strpos ( $ e -> getMessage ( ) , 'email_unique' ) !== false ) { Garp_Cli :: errorOut ( 'Error: this email address is already in use. ' . 'Maybe you meant to use Garp Admin make?' ) ; } else { throw $ e ; } } } }
Add a new admin to the system

@param array $args
@return void
Add a new admin to the system