CrugeSession.php 7.75 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
<?php

/**
 * This is the model class for table "CrugeSession".
 *
 * The followings are the available columns in table 'CrugeSession':
 * @property integer $idsession
 * @property integer $iduser
 * @property string $created
 * @property string $expire
 * @property integer $status
 * @property string $ipaddress
 * @property string $ipaddressout
 * @property string $logoutdate
 * @property integer $usagecount
 * @property string $lastusage
 * @author: Christian Salazar H. <christiansalazarh@gmail.com> @salazarchris74
 * @license protected/modules/cruge/LICENSE
 */
class CrugeSession extends CActiveRecord implements ICrugeSession
{

    /*
        @returns nueva instancia de CrugeSession
    */
    public static function create($iduser, $durationMins)
    {
        $model = new CrugeSession();
        $model->iduser = $iduser;
        $model->created = CrugeUtil::now();
        $model->lastusage = $model->created;
        $model->expire = CrugeUtil::makeExpirationDateTime($durationMins);
        $model->status = 1;
        $model->usagecount = 1;
        $model->ipaddress = CrugeUtil::getIpAddress();
        return $model;
    }

    /** que hacer cuando la sesion es reutilizada
    @returns void.
     */
    public function onReusage()
    {
        $this->usagecount++;
        $this->lastusage = CrugeUtil::now();
        $this->ipaddress = CrugeUtil::getIpAddress();
    }

    /*	almacena la sesion, que puede ser nueva o reutilizada
        @returns boolean. false=causa que la sesion no se asigne.
    */
    public function store()
    {
        $ret = false;
        if ($this->getIsNewRecord()) {
            $ret = $this->insert();
        } else {
            $ret = $this->update();
        }

        Yii::log(
            __CLASS__ . "::store() #" . $this->getPrimaryKey()
                . "\nresult=" . $ret . "\nerrorinfo:" . CHtml::errorSummary($this) . "\n"
                . "json: " . CJSON::encode($this) . "\n"
            ,
            "info"
        );

        return $ret;
    }


    /**
    es importante devolver el username porque este resultado podria evaluarse
    sobre un CAccessRule el cual comparara el nombre del user de su regla con este que
    aqui se retorna.

    @returns string Nombre del usuario (username) de esta sesion
     */
    public function getSessionName()
    {
        return $this->user->username;
    }

    /**
    @returns CrugeSession una instancia del modelo hallada por su IDSESSION
     */
    public static function loadModel($id)
    {
        return self::model()->findByPk($id);
    }

    /*
        @returns CrugeSession instancia de la sesion mas reciente hallada para este usuario
    */
    public static function findLast($iduser)
    {

        $model = null;

        // busca si este usuario tiene una sesion valida, para reutilizarla
        //
        $curSession = self::model()->findByAttributes(
            array('iduser' => $iduser),
            array('order' => 'idsession DESC')
        );
        Yii::log(__CLASS__ . "::findLast iduser={$iduser}\ncurSession:" . CJSON::encode($curSession), "info");

        $info = "";
        if ($curSession != null) {
            $info .= "[sesion encontrada]";
            if ($curSession->validateSession() == true) {
                $info .= "[sesion validada]";
                $model = $curSession;
            } else {
                $info .= "[sesion no validada]";
            }
        } else {
            $info .= "[sesion no hallada]";
        }

        Yii::log(__CLASS__ . "::findLast\nresult:" . $info, "info");

        return $model;
    }

    /**
    @returns Boolean indicando que la sesion es valida para ser utilizada y asignada
     */
    public function validateSession()
    {
        if ((1 * ($this->status)) != 1) {
            Yii::log("CrugeSession. validateSession. causa: estatus cerrado", "info");
            return false;
        }

        if ($this->isSessionExpired() == true) {
            Yii::log(
                "CrugeSession. validateSession. causa: sesion expirada. date.now="
                    . CrugeUtil::now(),
                "info"
            );
            return false;
        }

        return true;
    }

    /*
        @returns boolean true indicando que la sesion ha expirado
    */
    public function isSessionExpired()
    {
        return CrugeUtil::isExpired($this->expire) != '';
    }

    /* es invocado por CrugeWebUser via interfaz ICrugeSession

        @returns VOID
    */
    public function logout()
    {
        $this->logoutdate = CrugeUtil::now();
        $this->ipaddressout = CrugeUtil::getIpAddress();
        $this->status = 0;
    }

    /*
        @returns VOID
    */
    public function expiresession()
    {
        $this->status = 0;
    }

    public function tableName()
    {
        return CrugeUtil::getTableName('session');
    }

    public function getPrimaryKey()
    {
        return $this->idsession;
    }


    /**
     * Returns the static model of the specified AR class.
     * @return CrugeSession the static model class
     */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }


    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('iduser', 'required'),
            array('iduser, status', 'numerical', 'integerOnly' => true),
            array('ipaddress, ipaddressout', 'length', 'max' => 45),
            array('created, expire', 'required'),
            array('usagecount, lastusage', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('idsession, iduser, created, expire, status, ipaddress', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'user' => array(self::BELONGS_TO, 'crugestoreduser', 'iduser'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'idsession' => 'Idsession',
            'iduser' => 'Iduser',
            'sessionname' => CrugeTranslator::t("usuario"),
            'created' => CrugeTranslator::t("creacion"),
            'expire' => CrugeTranslator::t("expira"),
            'lastusage' => CrugeTranslator::t("ultimo uso"),
            'status' => CrugeTranslator::t("estado"),
            'usagecount' => CrugeTranslator::t("contador<br/>login"),
            'ipaddress' => 'Ipaddress',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('idsession', $this->idsession);
        $criteria->compare('iduser', $this->iduser);
        $criteria->compare('created', $this->created, true);
        $criteria->compare('expire', $this->expire, true);
        $criteria->compare('status', $this->status);
        $criteria->compare('ipaddress', $this->ipaddress, true);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
            'sort' => array(
                'defaultOrder' => array('idsession' => true),
            ),
        ));
    }
}