1<?php
2
3/* Copyright (c) 1998-2019 ILIAS open source, Extended GPL, see docs/LICENSE */
4
5/**
6 * Class ilObjGlossaryAccess
7 *
8 * @author Alex Killing <alex.killing@gmx.de>
9 */
10class ilObjGlossaryAccess extends ilObjectAccess
11{
12    /**
13     * @var ilObjUser
14     */
15    protected $user;
16
17    /**
18     * @var ilLanguage
19     */
20    protected $lng;
21
22    /**
23     * @var ilRbacSystem
24     */
25    protected $rbacsystem;
26
27    /**
28     * @var ilAccessHandler
29     */
30    protected $access;
31
32
33    /**
34     * Constructor
35     */
36    public function __construct()
37    {
38        global $DIC;
39
40        $this->user = $DIC->user();
41        $this->lng = $DIC->language();
42        $this->rbacsystem = $DIC->rbac()->system();
43        $this->access = $DIC->access();
44    }
45
46    /**
47    * checks wether a user may invoke a command or not
48    * (this method is called by ilAccessHandler::checkAccess)
49    *
50    * @param	string		$a_cmd		command (not permission!)
51    * @param	string		$a_permission	permission
52    * @param	int			$a_ref_id	reference id
53    * @param	int			$a_obj_id	object id
54    * @param	int			$a_user_id	user id (if not provided, current user is taken)
55    *
56    * @return	mixed		true, if everything is ok, message (string) when
57    *						access is not granted
58    */
59    public function _checkAccess($a_cmd, $a_permission, $a_ref_id, $a_obj_id, $a_user_id = "")
60    {
61        $ilUser = $this->user;
62        $lng = $this->lng;
63        $rbacsystem = $this->rbacsystem;
64        $ilAccess = $this->access;
65
66        if ($a_user_id == "") {
67            $a_user_id = $ilUser->getId();
68        }
69
70        switch ($a_permission) {
71            case "read":
72                if (!ilObjGlossaryAccess::_lookupOnline($a_obj_id)
73                    && !$rbacsystem->checkAccessOfUser($a_user_id, 'write', $a_ref_id)) {
74                    $ilAccess->addInfoItem(IL_NO_OBJECT_ACCESS, $lng->txt("offline"));
75                    return false;
76                }
77                break;
78
79            case "visible":
80                if (!ilObjGlossaryAccess::_lookupOnline($a_obj_id) &&
81                    (!$rbacsystem->checkAccessOfUser($a_user_id, 'write', $a_ref_id))) {
82                    $ilAccess->addInfoItem(IL_NO_OBJECT_ACCESS, $lng->txt("offline"));
83                    return false;
84                }
85                break;
86        }
87
88
89        return true;
90    }
91
92    /**
93     * get commands
94     *
95     * this method returns an array of all possible commands/permission combinations
96     *
97     * example:
98     * $commands = array
99     *	(
100     *		array("permission" => "read", "cmd" => "view", "lang_var" => "show"),
101     *		array("permission" => "write", "cmd" => "edit", "lang_var" => "edit"),
102     *	);
103     */
104    public static function _getCommands()
105    {
106        $commands = array(
107            array("permission" => "read", "cmd" => "view", "lang_var" => "show",
108                "default" => true),
109            array("permission" => "write", "cmd" => "edit", "lang_var" => "edit_content"),
110            array("permission" => "edit_content", "cmd" => "edit", "lang_var" => "edit_content"), // #11099
111            array("permission" => "write", "cmd" => "properties", "lang_var" => "settings")
112        );
113
114        return $commands;
115    }
116
117    //
118    // access relevant methods
119    //
120
121    /**
122    * check wether learning module is online
123    */
124    public static function _lookupOnline($a_id)
125    {
126        global $DIC;
127
128        $ilDB = $DIC->database();
129
130        $q = "SELECT * FROM glossary WHERE id = " .
131            $ilDB->quote($a_id, "integer");
132        $lm_set = $ilDB->query($q);
133        $lm_rec = $ilDB->fetchAssoc($lm_set);
134
135        return ilUtil::yn2tf($lm_rec["is_online"]);
136    }
137
138    /**
139     * Check wether learning module is online (legacy version)
140     *
141     * @deprecated
142     */
143    public static function _lookupOnlineStatus($a_ids)
144    {
145        global $DIC;
146
147        $ilDB = $DIC->database();
148
149        $q = "SELECT id, is_online FROM glossary WHERE " .
150            $ilDB->in("id", $a_ids, false, "integer");
151        $lm_set = $ilDB->query($q);
152        $status = [];
153        while ($r = $ilDB->fetchAssoc($lm_set)) {
154            $status[$r["id"]] = ilUtil::yn2tf($r["is_online"]);
155        }
156
157        return $status;
158    }
159
160
161    /**
162    * check whether goto script will succeed
163    */
164    public static function _checkGoto($a_target)
165    {
166        global $DIC;
167
168        $ilAccess = $DIC->access();
169
170        $t_arr = explode("_", $a_target);
171
172        if (($t_arr[0] != "glo" && $t_arr[0] != "git") || ((int) $t_arr[1]) <= 0) {
173            return false;
174        }
175
176        if ($t_arr[0] == "glo") {
177            if ($ilAccess->checkAccess("read", "", $t_arr[1]) ||
178                $ilAccess->checkAccess("visible", "", $t_arr[1])) {
179                return true;
180            }
181        }
182
183        if ($t_arr[0] == "git") {
184            if ($t_arr[2] > 0) {
185                $ref_ids = array($t_arr[2]);
186            } else {
187                // determine learning object
188                $glo_id = ilGlossaryTerm::_lookGlossaryID($t_arr[1]);
189                $ref_ids = ilObject::_getAllReferences($glo_id);
190            }
191            // check read permissions
192            foreach ($ref_ids as $ref_id) {
193                // Permission check
194                if ($ilAccess->checkAccess("read", "", $ref_id)) {
195                    return true;
196                }
197            }
198        }
199
200        return false;
201    }
202}
203