Changeset 16513

Show
Ignore:
Timestamp:
05/30/08 16:59:26 (5 months ago)
Author:
adrenalin
Message:

Moved the object locking, unlocking and lock checking to
midcom.helper.metadata and changed the privilege to midcom:unlock.
Lock timeout is now in minutes rather than secods.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/midcom/midcom.admin.user/config/schemadb_group.inc

    r16487 r16513  
    150150            'storage'     => null, 
    151151        ), 
    152         'break_lock' => array 
     152        'unlock' => array 
    153153        ( 
    154             'title'       => 'enable breaking of the editing lock', 
     154            'title'       => 'enable unlocking locked objects', 
    155155            'type'        => 'privilege', 
    156156            'type_config' => array 
    157157            ( 
    158                 'privilege_name' => 'midcom.helper.datamanager2:break_lock', 
     158                'privilege_name' => 'midcom:unlock', 
    159159                'assignee'       => 'SELF', 
    160                 'classname'      => 'midcom_helper_datamanager2_controller', 
     160                'classname'      => 'midcom_services_auth', 
    161161            ), 
    162162            'widget'      => 'privilege', 
  • trunk/midcom/midcom.core/midcom/config/midcom_config.php

    r16498 r16513  
    495495$GLOBALS['midcom_config_default']['metadata_approval'] = false; 
    496496$GLOBALS['midcom_config_default']['metadata_scheduling'] = false; 
     497$GLOBALS['midcom_config_default']['metadata_lock_timeout'] = 60;    // Time in minutes 
    497498$GLOBALS['midcom_config_default']['staging2live_staging'] = false; 
    498499 
  • trunk/midcom/midcom.core/midcom/helper/metadata.php

    r14773 r16513  
    735735        return $this->_translations; 
    736736    } 
     737     
     738    /** 
     739     * Check if the requested object is locked 
     740     *  
     741     * @static 
     742     * @access public 
     743     * @param mixed &$object    MgdSchema object 
     744     * @return boolean          True if the object is locked, false if it isn't 
     745     */ 
     746    function is_locked() 
     747    { 
     748        // Object hasn't been marked to be edited 
     749        if ($this->get('locked') === 0) 
     750        { 
     751            return false; 
     752        } 
     753         
     754        if ($this->get('locked') - time() > $GLOBALS['midcom_config']['metadata_lock_timeout'] * 60) 
     755        { 
     756            return true; 
     757        } 
     758         
     759        // Get the person who locked the this 
     760        // Lock was created by the user, allow unquestionable editing 
     761        if (   isset($_MIDCOM->auth->user) 
     762            && isset($_MIDCOM->auth->user->guid) 
     763            && $this->get('locker') !== $_MIDCOM->auth->user->guid) 
     764        { 
     765            return true; 
     766        } 
     767         
     768        // Locks checked, this isn't locked 
     769        return false; 
     770    } 
     771     
     772     
     773    /** 
     774     * Set the object lock 
     775     *  
     776     * @access public 
     777     * @param int $timeout   Length of the lock timeout 
     778     * @param String $user   GUID of the midgard_person object 
     779     * @return boolean       Indicating success 
     780     */ 
     781    function lock($timeout = null, $user = null) 
     782    { 
     783        if (!$timeout) 
     784        { 
     785            $timeout = $GLOBALS['midcom_config']['metadata_lock_timeout']; 
     786        } 
     787         
     788        if (!$user) 
     789        { 
     790            $this->set('locker', $_MIDCOM->auth->user->guid); 
     791        } 
     792        else 
     793        { 
     794            $this->set('locker', $user); 
     795        } 
     796         
     797        // Update failed, return false 
     798        if (!$this->set('locked', time() + $timeout * 60)) 
     799        { 
     800            return false; 
     801        } 
     802         
     803        return true; 
     804    } 
     805 
     806    /** 
     807     * Unlock the object 
     808     *  
     809     * @access public 
     810     * @param boolean    Indicating success 
     811     */ 
     812    function unlock() 
     813    { 
     814        if (!$_MIDCOM->auth->can_user_do('midcom:unlock', null, 'midcom_services_auth', 'midcom')) 
     815        { 
     816            return false; 
     817        } 
     818         
     819         
     820        if (   !$this->set('locker', '') 
     821            || !$this->set('locked', '')) 
     822        { 
     823            return false; 
     824        } 
     825         
     826        return true; 
     827    } 
    737828} 
    738829?> 
  • trunk/midcom/midcom.core/midcom/services/auth.php

    r15955 r16513  
    674674                'midcom:ajax' => MIDCOM_PRIVILEGE_DENY, 
    675675                'midcom:centralized_toolbar' => MIDCOM_PRIVILEGE_DENY, 
     676                'midcom:unlock' => MIDCOM_PRIVILEGE_DENY, 
    676677            ) 
    677678        ); 
  • trunk/midcom/midcom.core/midcom/services/metadata.php

    r15379 r16513  
    227227        // HTML generator information 
    228228        $_MIDCOM->add_meta_head( 
    229             array( 
     229            array 
     230            ( 
    230231                'name' => 'generator', 
    231232                'content' => 'Midgard/' . mgd_version() . ' MidCOM/' . $GLOBALS['midcom_version'] . ' PHP/' . phpversion() 
     
    235236        // PermaLink into machine-detectable format 
    236237        $_MIDCOM->add_link_head( 
    237             array( 
     238            array 
     239            ( 
    238240                'rel' => 'bookmark', 
    239241                'href' => $request_metadata['permalink'] 
     
    243245        // Last revision time for the entire page 
    244246        $_MIDCOM->add_meta_head( 
    245             array( 
     247            array 
     248            ( 
    246249                'name' => 'lastupdated', 
    247250                'content' => @gmdate('Y-m-d H:i:s\Z', $request_metadata['lastmodified']) 
  • trunk/midcom/midcom.helper.datamanager2/controller.php

    r16485 r16513  
    254254    function display_form() 
    255255    { 
    256         if (midcom_helper_datamanager2_controller::is_locked($this->datamanager->storage->object, $this->lock_timeout)) 
     256        // Get the metadata object 
     257        $metadata = $this->datamanager->storage->object->get_metadata(); 
     258         
     259        if ($metadata->is_locked()) 
    257260        { 
    258261            $this->show_remove_lock(); 
     
    277280        else 
    278281        { 
    279             $user = $this->datamanager->storage->object->get_parameter('midcom.helper.datamanager2.lock', 'user'); 
    280             $expires = strtotime($this->datamanager->storage->object->get_parameter('midcom.helper.datamanager2.lock', 'expires')); 
    281              
    282             $person = new midcom_db_person($user); 
     282            $person = new midcom_db_person($this->datamanager->storage->object->metadata->locker); 
    283283            ?> 
    284284                <div class="midcom_helper_datamanager2_remove_lock"> 
     
    286286                    <p> 
    287287                        <?php echo sprintf($this->_l10n->get('this object was locked by %s'), $person->name); ?>. 
    288                         <?php echo sprintf($this->_l10n->get('lock will expire on %s'), strftime('%x %X', $expires)); ?>. 
     288                        <?php echo sprintf($this->_l10n->get('lock will expire on %s'), strftime('%x %X', $this->datamanager->storage->object->metadata->locked)); ?>. 
    289289                    </p> 
    290290            <?php 
    291             if ($_MIDCOM->auth->can_user_do('midcom.helper.datamanager2:break_lock', null, 'midcom_helper_datamanager2_controller', 'midcom.helper.datamanager2')) 
     291            if ($_MIDCOM->auth->can_user_do('midcom:unlock', null, 'midcom_services_auth', 'midcom.core')) 
    292292            { 
    293293                echo "<form method=\"post\">\n"; 
     
    303303        } 
    304304    } 
    305      
    306     /** 
    307      * Check if the object is being edited elsewhere 
    308      *  
    309      * @static 
    310      * @access public 
    311      * @param mixed $guid      Object or GUID of the object 
    312      * @return boolean         True if the object is locked 
    313      */ 
    314     function is_locked($target, $timeout = 0) 
    315     { 
    316         // Remove the object lock if applicable 
    317         if (   isset($_REQUEST['midcom_helper_datamanager2_remove_lock']) 
    318             && isset($_REQUEST['midcom_helper_datamanager2_object'])) 
    319         { 
    320             // Get the object 
    321             $object = $_MIDCOM->dbfactory->get_object_by_guid($_POST['midcom_helper_datamanager2_object']); 
    322              
    323             // Remove the lock, if permission is granted 
    324             if ($_MIDCOM->auth->can_user_do('midcom.helper.datamanager2:break_lock', null, 'midcom_helper_datamanager2_controller', 'midcom.helper.datamanager2')) 
    325             { 
    326                 midcom_helper_datamanager2_controller::set_lock($object, $timeout); 
    327             } 
    328             else 
    329             { 
    330                 $_MIDCOM->uimessages->add($_MIDCOM->i18n->get_string('midcom.helper.datamanager2', 'midcom.helper.datamanager2'), $_MIDCOM->i18n->get_string('permission denied', 'midcom'), 'error'); 
    331             } 
    332         } 
    333          
    334         // Check that we have an object at disposal 
    335         if (is_object($target)) 
    336         { 
    337             $object =& $target; 
    338         } 
    339         else 
    340         { 
    341             $object = $_MIDCOM->dbfactory->get_object_by_guid($target); 
    342         } 
    343          
    344         // Couldn't get the object. Different error handling here? Controller should have already alarmed the user, though... 
    345         if (   !$object 
    346             || !$object->guid) 
    347         { 
    348             return false; 
    349         } 
    350          
    351         $expires = (int) strtotime($object->get_parameter('midcom.helper.datamanager2.lock', 'expires')); 
    352          
    353         // Object not locked, allow editing 
    354         if (!$expires) 
    355         { 
    356             return false; 
    357         } 
    358          
    359         // Get the person who locked the object 
    360         $user = $object->get_parameter('midcom.helper.datamanager2.lock', 'user'); 
    361          
    362         // Lock was created by the user, allow unquestionable editing 
    363         if (   isset($_MIDCOM->auth->user) 
    364             && isset($_MIDCOM->auth->user->guid) 
    365             && $user === $_MIDCOM->auth->user->guid) 
    366         { 
    367             return false; 
    368         } 
    369          
    370         // Object lock is no longer valid 
    371         if (time() > $expires) 
    372         { 
    373             return false; 
    374         } 
    375          
    376         // Lock checked, object locked 
    377         return true; 
    378     } 
    379      
    380     /** 
    381      * Set the object lock 
    382      *  
    383      * @static 
    384      * @access public 
    385      * @param mixed $object    Object that should be locked 
    386      * @param int $timeout     Length of the lock timeout 
    387      */ 
    388     function set_lock($object, $timeout) 
    389     { 
    390         if ((int) $timeout < 1) 
    391         { 
    392             $object->set_parameter('midcom.helper.datamanager2.lock', 'user', ''); 
    393             $object->set_parameter('midcom.helper.datamanager2.lock', 'expires', ''); 
    394             return true; 
    395         } 
    396          
    397         $object->set_parameter('midcom.helper.datamanager2.lock', 'user', $_MIDCOM->auth->user->guid); 
    398         $object->set_parameter('midcom.helper.datamanager2.lock', 'expires', strftime('%Y-%m-%d %H:%M:%S', time() + $timeout)); 
    399          
    400         return true; 
    401     } 
    402305} 
    403306?> 
  • trunk/midcom/midcom.helper.datamanager2/controller/simple.php

    r16485 r16513  
    8787 
    8888        $result = $this->formmanager->process_form(); 
     89        $metadata = $this->datamanager->storage->object->get_metadata(); 
    8990         
    9091        // Remove the lock 
     
    9293            || $result === 'cancel') 
    9394        { 
    94             midcom_helper_datamanager2_controller::set_lock($this->datamanager->storage->object, 0); 
     95            $metadata->unlock(); 
    9596        } 
    9697        // or set it, if needed 
    97         elseif (!midcom_helper_datamanager2_controller::is_locked($this->datamanager->storage->object, $this->lock_timeout)) 
     98        elseif (!$metadata->is_locked()) 
    9899        { 
    99             midcom_helper_datamanager2_controller::set_lock($this->datamanager->storage->object, $this->lock_timeout); 
     100            $metadata->lock(); 
    100101        } 
    101102         
  • trunk/midcom/midcom.helper.datamanager2/datamanager.php

    r14806 r16513  
    444444        echo "</div>\n"; 
    445445    } 
    446  
    447  
    448446} 
    449  
    450447?> 
  • trunk/midcom/midcom.helper.datamanager2/formmanager.php

    r16485 r16513  
    10171017                return 'preview'; 
    10181018                 
     1019            case (array('midcom_helper_datamanager2_remove_lock', $_REQUEST)): 
     1020                if (isset($_REQUEST['midcom_helper_datamanager2_object'])) 
     1021                { 
     1022                    $object = $_MIDCOM->dbfactory->get_object_by_guid($_REQUEST['midcom_helper_datamanager2_object']); 
     1023                     
     1024                    // Failed to get the object 
     1025                    if (   !$object 
     1026                        || !$object->guid) 
     1027                    { 
     1028                        debug_push_class(__CLASS__, __FUNCTION__); 
     1029                        debug_add("Could not get the object that was requested for unlocking, perhaps it doesn't exist anymore.", MIDCOM_LOG_ERROR); 
     1030                        debug_add('Last Midgard error was ' . mgd_errstr(), MIDCOM_LOG_ERROR); 
     1031                        debug_pop(); 
     1032                         
     1033                        $_MIDCOM->generate_error(MIDCOM_ERRCRIT, 'Failed to get the object for unlocking, see error level log for details'); 
     1034                        // This will exit 
     1035                    } 
     1036                     
     1037                    $metadata = $object->get_metadata(); 
     1038                     
     1039                    // Remove the lock, if permission is granted 
     1040                    if ($_MIDCOM->auth->can_user_do('midcom:unlock', null, 'midcom_services_auth', 'midcom.core')) 
     1041                    { 
     1042                        $metadata->unlock(); 
     1043                    } 
     1044                    else 
     1045                    { 
     1046                        $_MIDCOM->uimessages->add($_MIDCOM->i18n->get_string('midcom.helper.datamanager2', 'midcom.helper.datamanager2'), $_MIDCOM->i18n->get_string('permission denied', 'midcom'), 'error'); 
     1047                    } 
     1048                } 
    10191049            default: 
     1050        // Remove the this lock if applicable 
    10201051                return 'edit'; 
    10211052        }