aboutsummaryrefslogtreecommitdiff
path: root/lib/filesystem/PermissionEnabledFolder.php
blob: 36f98f6933c7cc66456d2c29d550bbdb89d65a0f (plain)
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
<?php
/**
 * PermissionEnabledFolder.php
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * @author    André Noack <noack@data-quest.de>
 * @copyright 2016 Stud.IP Core-Group
 * @license   http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @category  Stud.IP
 */
class PermissionEnabledFolder extends StandardFolder
{
    protected $permission = 7;
    protected $perms = ['x' => 1, 'w' => 2, 'r' => 4, 'f' => 8];
    protected $must_have_perm;

    public static function availableInRange(SimpleORMap|string $range_id_or_object, string $user_id): bool
    {
        return false;
    }

    public static function getTypeName(): string
    {
        return _('Ordner mit Zugangsbeschränkung');
    }

    public function __construct($folderdata = null)
    {
        parent::__construct($folderdata);

        if (isset($this->folderdata['data_content']['permission'])) {
            $this->permission = $this->folderdata['data_content']['permission'];
        }

        $this->must_have_perm = $this->range_type === 'course' ? 'tutor' : 'autor';
    }

    public function getPermissionString()
    {
        $perms = $this->perms;
        array_pop($perms);

        $r = array_flip($perms);
        foreach($perms as $v => $p) {
            if (!($this->permission & $p)) {
                $r[$p] = '-';
            }
        }
        return implode(array_reverse($r));
    }

    public function checkPermission($perm, $user_id = null)
    {
        if ($user_id && is_object($GLOBALS['perm']) && $GLOBALS['perm']->have_studip_perm($this->must_have_perm, $this->range_id, $user_id)) {
            return true;
        }

        return (bool)($this->permission & $this->perms[$perm]);
    }

    public function getIcon(string $role = Icon::DEFAULT_ROLE): Icon
    {
        $shape = $this->is_empty
               ? 'folder-lock-empty'
               : 'folder-lock-full';
        return Icon::create($shape, $role);
    }

    public function isVisible(string $user_id = null): bool
    {
        return $this->checkPermission('x', $user_id)
            && parent::isVisible($user_id);
    }

    public function isReadable(string $user_id = null): bool
    {
        return $this->checkPermission('r', $user_id)
            && parent::isReadable($user_id);
    }

    public function isWritable(string $user_id = null): bool
    {
        return $this->checkPermission('w', $user_id)
            && parent::isWritable($user_id);
    }

    public function isSubfolderAllowed(string $user_id): bool
    {
        return $this->checkPermission('f', $user_id);
    }

    public function getDescriptionTemplate(): \Flexi\Template|string|null
    {
        $template = $GLOBALS['template_factory']->open('filesystem/permission_enabled_folder/description.php');

        $template->type   = self::getTypeName();
        $template->folder = $this;
        $template->folderdata = $this->folderdata;
        return $template;
    }

    public function validateUpload(FileType $file, string $user_id): ?string
    {
        if (!$this->isWritable($user_id)) {
            return _('Der Dateiordner ist nicht beschreibbar.');
        }

        return parent::validateUpload($file, $user_id);
    }

    /**
     * @return FileType[]
     */
    public function getFiles(): array
    {
        return array_filter(parent::getFiles(), function($file) {
            return $this->isFileVisible($file->getFileRef(), $GLOBALS['user']->id);
        });
    }

    /**
     * Determines if a user may see the file.
     * @param FileRef|string $fileref_or_id
     * @param string $user_id
     * @return bool
     */
    public function isFileVisible($fileref_or_id, $user_id)
    {
        return $this->isReadable($user_id);
    }

    /**
     * @param FileRef $file_ref
     * @param string  $user_id
     * @return bool
     */
    public function isFileDownloadable(FileRef $file_ref, string $user_id): bool
    {
        $fileref = FileRef::find($file_ref);

        if ($fileref) {
            if ($this->isVisible($user_id) && $this->isFileVisible($fileref, $user_id)) {
                return $fileref->terms_of_use->isDownloadable($this->range_id, $this->range_type, true, $user_id);
            }
        }

        return false;
    }

    /**
     * @see FolderType::copySettings()
     */
    public function copySettings(): array
    {
        return [
            'description' => $this->description,
            'data_content' => $this->data_content
        ];
    }

    public function countDownloads(?FileRef $ref = null): bool
    {
        return ($this->permission & $this->perms['r'])
            && parent::countDownloads($ref);
    }
}