aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/UserFilterFields/MassMail/MassMailPermissionFilter.php
blob: d4627489c0f263af526e883a08fa2d1e351e751c (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
<?php

/**
 * PermissionCondition.php
 *
 * All conditions concerning the semester of study in Stud.IP can be specified here.
 *
 * 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      Elmar Ludwig <elmar.ludwig@uos.de>
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @category    Stud.IP
 */
namespace UserFilterFields\MassMail;

use UserFilterFields\PermissionCondition;
use User;
use DBManager;
use MassMail\MassMailPermission;

class MassMailPermissionFilter extends PermissionCondition
{

    public string $target = '';

    public static $sortOrder = 10;

    /**
     * @see \UserFilterField::getTargets()
     */
    public static function getTargets()
    {
        return ['students', 'employees'];
    }

    /**
     * @see UserFilterField::__construct
     */
    public function __construct($fieldId = '')
    {
        $this->userDataDbTable = 'auth_user_md5';
        $this->userDataDbField = 'perms';

        parent::__construct($fieldId);

        $this->validValues = [
            'autor' => _('Student/in'),
            'tutor' => _('Tutor/in'),
            'dozent' => _('Lehrende/r')
        ];
    }

    /**
     * Get this field's display name.
     *
     * @return String
     */
    public function getName()
    {
        return _('Globaler Status');
    }

    /**
     * Gets all users with given gender.
     *
     * @return array All users that are affected by the current condition
     * field.
     */
    public function getUsers($restrictions = array())
    {
        $users = [];
        if (MassMailPermission::has(User::findCurrent()->id, true)) {
            $users = DBManager::get()->fetchFirst("SELECT DISTINCT `user_id` " .
                "FROM `" . $this->userDataDbTable . "` " .
                "WHERE `" . $this->userDataDbField . "`" . $this->compareOperator .
                "?", [$this->value]);
        } else if (MassMailPermission::has(User::findCurrent()->id)) {

            $allowed = MassMailPermission::getForUser(User::findCurrent());

            $sql = "SELECT DISTINCT `" . $this->userDataDbTable . "`.`user_id` FROM `" . $this->userDataDbTable . "` ";
            $where = "WHERE `" . $this->userDataDbTable . "`.`" . $this->userDataDbField . "`" . $this->compareOperator . ":value";
            $parameters = ['value' => $this->value];

            switch ($this->target) {
                case 'employees':
                    $sql .= "JOIN `user_inst` USING (`user_id`) ";
                    $where .= " AND `user_inst`.`Institut_id` IN (:institutes) AND `user_inst`.`inst_perms` IN (:perms)";
                    $parameters['institutes'] = $allowed['allowed_institutes'];
                    $parameters['perms'] = ['autor', 'tutor', 'dozent', 'admin'];
                    break;
                case 'students':
                default:
                    $sql .= "JOIN `user_studiengang` USING (`user_id`) ";
                    $where .= " AND (
                            `user_studiengang`.`abschluss_id` IN (:degrees)
                            OR `user_studiengang`.`fach_id` IN (:subjects)
                        )";
                    $parameters['degrees'] = $allowed['allowed_degrees'];
                    $parameters['subjects'] = $allowed['allowed_subjects'];
            }

            $users = DBManager::get()->fetchFirst($sql.$where, $parameters);
        }

        return $users;
    }
}