Perforce Chronicle 2012.2/486814
API Documentation

Setup_Form_Administrator Class Reference

This is the Perforce server setup form. More...

Inheritance diagram for Setup_Form_Administrator:
P4Cms_Form

List of all members.

Public Member Functions

 getServerType ()
 Retrieve the current serverType value.
 init ()
 Defines the elements that make up the Administrator form.
 isP4LicenseQuotaSufficient ($serverLicense, $users)
 Check the license quota.
 isValid ($data)
 Override isValid to check connection parameters.
 setP4Port ($port)
 Set the port of the Perforce server for this site.
 setServerType ($type)
 Set the type of Perforce server for this site (local/existing).

Public Attributes

const DEFAULT_ADMIN_NAME = 'admin'

Detailed Description

This is the Perforce server setup form.

Copyright:
2011-2012 Perforce Software. All rights reserved
License:
Please see LICENSE.txt in top-level folder of this distribution.
Version:
2012.2/486814

Member Function Documentation

Setup_Form_Administrator::getServerType ( )

Retrieve the current serverType value.

Returns:
string the current serverType.
    {
        return $this->_serverType;
    }
Setup_Form_Administrator::init ( )

Defines the elements that make up the Administrator form.

Called automatically when the form object is created.

    {
        // form should use p4cms-ui styles.
        $this->setAttrib('class', 'p4cms-ui administrator-form');

        // form should submit on enter
        $this->setAttrib('submitOnEnter', true);

        // set the method for the display form to POST
        $this->setMethod('post');

        // getRequestHost will return false for non-http requests;
        // fallback to hostname if this is the case or if we got an IP
        // back (which wouldn't produce a valid default email address)
        $defaultHost = Setup_Form_Site::getRequestHost();
        if (!$defaultHost || preg_match('/^[0-9\.]+$/', $defaultHost)) {
            $defaultHost = gethostname();
        }

        $defaultEmail = static::DEFAULT_ADMIN_NAME . '@' . $defaultHost;

        // add a field to collect the perforce user, updating email field on change, but only
        // if the user hasn't customized the email
        $this->addElement(
            'text',
            'user',
            array(
                'label'         => 'User Name',
                'required'      => true,
                'validators'    => array(array('SpecName')),
                'onChange'      => <<<EOT
                if (!dojo.byId('email')) return;

                var email = dojo.byId('email').value;

                if (email == ''
                    || (this.previousName == undefined && email == '$defaultEmail')
                    || email == this.previousName+'@$defaultHost'
                 ) {
                    dojo.byId('email').value = this.value + '@$defaultHost';
                 }

                 this.previousName = this.value;
EOT
            )
        );

        if ($this->getServerType() == Setup_Form_Storage::SERVER_TYPE_NEW) {
            // add a field to collect the admin password
            $this->addElement(
                'text',
                'email',
                array(
                    'label'         => 'Email',
                    'required'      => true,
                    'value'         => $defaultEmail,
                    'validators'    => array(
                        array('EmailAddress', false, Zend_Validate_Hostname::ALLOW_LOCAL)
                    )
                )
            );
        }

        // add a field to collect the perforce password.
        $this->addElement(
            'password',
            'password',
            array(
                'label'         => 'Password',
                'value'         => '',
            )
        );

        if ($this->getServerType() == Setup_Form_Storage::SERVER_TYPE_NEW) {
            // for new servers, provide a default username, which the user can replace
            $element = $this->getElement('user');
            $element->setValue(static::DEFAULT_ADMIN_NAME);

            // for new servers, the security counter will be set to 2, requiring a strong password
            $this->getElement('password')
                 ->addValidator('StrongPassword')
                 ->setRequired(true);

            // add a field to confirm the password.
            $this->addElement(
                'password',
                'passwordConfirm',
                array(
                    'label'     => 'Confirm Password',
                    'value'     => '',
                    'required'  => true,
                )
            );
        }


        // add the submit button
        $this->addElement(
            'SubmitButton',
            'continue',
            array(
                'label'     => 'Continue',
                'class'     => 'button-large preferred',
                'ignore'    => true
            )
        );
        $this->addElement(
            'SubmitButton',
            'goback',
            array(
                'label'     => 'Go Back',
                'class'     => 'button-large',
                'ignore'    => true
            )
        );

        // put the button in a fieldset.
        $this->addDisplayGroup(
            array('continue', 'goback'),
            'buttons',
            array('class' => 'buttons')
        );
    }
Setup_Form_Administrator::isP4LicenseQuotaSufficient ( serverLicense,
users 
)

Check the license quota.

Parameters:
string$serverLicenseserverLicense field from $p4->info()
int$userscount of users from $p4->users()
Returns:
boolean true if license has room for more users
    {
        $licenses = false;
        if (preg_match("/([0-9]+) users?/", $serverLicense, $matches)) {
            $licenses = intval($matches[1]);
        }

        if ($licenses && $licenses <= $users) {
            $this->getElement('user')->addError(
                "Can't create a new site on this server. All available licenses are in use."
            );

            return false;
        }

        return true;
    }
Setup_Form_Administrator::isValid ( data)

Override isValid to check connection parameters.

Parameters:
array$datathe field values to validate.
Returns:
boolean true if the form values are valid.

Reimplemented from P4Cms_Form.

    {
        // do basic validation.
        if (!parent::isValid($data)) {
            return false;
        }

        // if serverType is 'new', nothing more to validate
        if (isset($this->_serverType)
            && $this->_serverType === Setup_Form_Storage::SERVER_TYPE_NEW
        ) {
            // make sure that the password and confirmation match
            $password = isset($data['password']) ? $data['password'] : null;
            $confirm  = isset($data['passwordConfirm']) ? $data['passwordConfirm'] : null;
            if ($password != $confirm) {
                $this->getElement('passwordConfirm')->addError("The two passwords do not match.");
                return false;
            }
            return true;
        }

        // try to login to perforce to test the connection parameters.
        try {
            $p4 = P4_Connection::factory(
                $this->_p4Port,
                $this->getValue('user'),
                null,
                $this->getValue('password')
            );
            $p4->login();
        } catch (P4_Connection_ConnectException $e) {
            $this->getElement('user')->addError("Unable to connect to server on '" . $this->_p4Port . "'.");
            return false;
        } catch (P4_Connection_LoginException $e) {
            if ($e->getCode() === P4_Connection_LoginException::IDENTITY_NOT_FOUND) {
                $this->getElement('user')->addError("Login failed. Unknown user.");
            } else if ($e->getCode() === P4_Connection_LoginException::CREDENTIAL_INVALID) {
                $this->getElement('password')->addError("Login failed. Invalid password.");
            } else {
                $this->getElement('user')->addError(
                    "Login failed. Please try again with a different username/password,"
                    . " or review the application log for more details."
                );
            }
            return false;
        }

        // check access level (must be super).
        try {
            $p4->run('protect', '-o');
        } catch (P4_Connection_CommandException $e) {
            if (stristr($e->getMessage(), "You don't have permission")) {
                $this->getElement('user')->addError("This user does not have permission to create sites.");
                return false;
            } else {
                throw $e;
            }
        }

        // check license quota.
        $result = $p4->run('users');
        $users  = count($result->getData());
        $info   = $p4->getInfo();
        if (!$this->isP4LicenseQuotaSufficient($info['serverLicense'], $users)) {
            return false;
        }

        // verify the 'chronicle' user is available if this is the initial setup.
        // if the application has no perforce resource, we assume initial setup.
        $bootstrap = Zend_Controller_Front::getInstance()->getParam("bootstrap");
        $perforce  = $bootstrap ? $bootstrap->getResource('perforce') : null;
        if (!$perforce && P4_User::exists(Setup_IndexController::P4D_USER, $p4)) {
            $this->getElement('user')->addError(
                "Can't create a new site on this server. The 'chronicle' user is already in use."
            );
            return false;
        }

        // passed all checks.
        return true;
    }
Setup_Form_Administrator::setP4Port ( port)

Set the port of the Perforce server for this site.

Parameters:
string$portthe perforce server to connect to.
    {
        $this->_p4Port = $port;
    }
Setup_Form_Administrator::setServerType ( type)

Set the type of Perforce server for this site (local/existing).

Parameters:
string$typethe type of server to connect to.
Returns:
Content_Form_Content provide a fluent interface.
    {
        $this->_serverType = $type;
        return $this;
    }

Member Data Documentation


The documentation for this class was generated from the following file: