Do not user unlock thread if thread being locked by a staff

thuylinh

Active member
Registered
Joined
Feb 5, 2020
Messages
36
Points
18

Reputation:

I have code like this, but when user locks own thread he can't unlock own thread. I want members to be able to unlock their thread and if that thread is locked by moderators then members will not be able to unlock their thread, how do I fix the code?

Code:
public function canLockUnlock(&$error = null)
    {
        static $modLogs = [];

        $allowed = parent::canLockUnlock($error);
        if (!$allowed) {
            if ($this->hasTOPPermission('top_lockUnlock')) {
                if (!$this->discussion_open) {
                    if (!array_key_exists($this->thread_id, $modLogs)) {
                        $modLogs[$this->thread_id] = /** @var ModeratorLog|null $modLog */
                        $modLog = $this->finder('XF:ModeratorLog')
                            ->where('content_type', $this->getEntityContentType())
                            ->where('content_id', $this->thread_id)
                            ->where('action', ['lock', 'unlock'])
                            ->order('log_date', 'DESC')
                            ->fetchOne();
                    }
                    /** @var ModeratorLog|null $modLog */
                    $modLog = $modLogs[$this->thread_id];

                    if ($modLog !== null && $modLog->action === 'lock') {
                        // do not user unlock thread if thread being locked by a staff
                        // see the report bug: https://xenforo.com/community/threads/172000/
                        $error = \XF::phrase('you_may_not_perform_this_action_because_discussion_is_closed');

                        return false;
                    }
                    
                }

                return true;
            }
 
        }

        return $allowed;
    }
Thanks you!
 
Top