<?php

class Application_Model_DbTable_MigrateRoles extends Unl_Db_Table_Abstract
{
    const ROLE_NONE      = 'none';
    const ROLE_ADMIN     = 'admin';
    const ROLE_READALL   = 'readall';
    const ROLE_UNARCHIVE = 'unarchive';
    const ROLE_BTMIGRATE = 'btmigrate';
    const ROLE_QMIGRATE  = 'qmigrate';
    const ROLE_STORAGE   = 'storage';
    const ROLE_CUSTSERV  = 'custserv';
    
    protected $_name = 'migrateroles';
    protected $_primary = 'username';

    static public function getCurrentRole()
    {
        static $role = NULL;
        
        if ($role) {
            return $role;
        }
        
        if (!Zend_Auth::getInstance()->hasIdentity()) {
            $role = self::ROLE_NONE;
            return $role;
        }
        
        $select = self::getInstance()->select()
            ->where('username = ?', Zend_Auth::getInstance()->getIdentity());
        $record = self::getInstance()->fetchRow($select);
        if (!$record) {
            $role = self::ROLE_NONE;
            return $role;
        }
        
        $role = $record->role;
        return $role;
    }

    static public function currentUserCanView()
    {
        $role = self::getCurrentRole();
        if (!in_array($role, array(self::ROLE_NONE))) {
            return TRUE;
        }
    
        return FALSE;
    }
    static public function currentUserCanEdit()
    {
        $role = self::getCurrentRole();
        if (!in_array($role, array(self::ROLE_NONE, self::ROLE_READALL))) {
            return TRUE;
        }
        
        return FALSE;
    }
}