aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Routes/Blubber/Authority.php
blob: bfec8da6a1b6c6e5b9d9b9f127fcf691eb9a9037 (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
<?php

namespace JsonApi\Routes\Blubber;

use BlubberComment;
use BlubberThread;
use Course;
use User;

class Authority
{
    public static function canShowBlubberThread(User $user, BlubberThread $resource)
    {
        return self::userIsAuthor($user) && $resource->isReadable($user->id);
    }

    public static function canEditBlubberThread(User $user, BlubberThread $resource): bool
    {
        return self::canShowBlubberThread($user, $resource);
    }

    public static function canCreatePrivateBlubberThread(User $user)
    {
        return self::userIsAuthor($user);
    }

    public static function canCreateCourseBlubberThread(User $user, Course $course)
    {
        return self::userIsTeacher($user, $course);
    }

    public static function canEditCourseBlubberThread(User $user, Course $course)
    {
        return self::userIsTeacher($user, $course);
    }

    public static function canCreateComment(User $user, BlubberThread $resource)
    {
        return self::userIsAuthor($user) && $resource->isCommentable($user->id);
    }

    public static function canDeleteComment(User $user, BlubberComment $resource)
    {
        return self::canEditComment($user, $resource);
    }

    public static function canEditComment(User $user, BlubberComment $resource)
    {
        return self::userIsAuthor($user) && $resource->isWritable($user->id);
    }

    public static function canIndexComments(User $user, ?BlubberThread $resource = null)
    {
        return isset($resource)
            ? self::canShowBlubberThread($user, $resource)
            : self::userIsAuthor($user);
    }

    public static function canShowComment(User $user, BlubberComment $resource)
    {
        return self::canShowBlubberThread($user, $resource->thread);
    }

    /**
     * @SuppressWarnings(PHPMD.Superglobals)
     */
    private static function userIsAuthor(User $user)
    {
        return $GLOBALS['perm']->have_perm('autor', $user->id);
    }

    /**
     * @SuppressWarnings(PHPMD.Superglobals)
     */
    private static function userIsTeacher(User $user, Course $course)
    {
        return $GLOBALS['perm']->have_studip_perm('tutor', $course->id, $user->id);
    }
}