aboutsummaryrefslogtreecommitdiff
path: root/lib/models/BannerRoles.php
blob: a460986f861accd0c459c46ae339aa6efe5d02e6 (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
<?php

/**
 * BannerRoles.php - model class for the banner roles
 *
 * 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      Sebastian Biller <s.biller@tu-braunschweig.de>
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @category    Stud.IP
 * @package     admin
 * @since       5.1
 *
 * @property array $id alias for pk
 * @property string $ad_id database column
 * @property int $roleid database column
 * @property Banner $banner_ads belongs_to Banner
 */

class BannerRoles extends SimpleORMap
{
    protected static function configure($config = [])
    {
        $config['db_table'] = 'banner_roles';

        $config['belongs_to']['banner_ads'] = [
            'class_name'  => Banner::class,
            'foreign_key' => 'ad_id',
        ];

        parent::configure($config);
    }

    public static function checkUserAccess($ad_id, $user_id = null)
    {
        $user_id = $user_id ?: $GLOBALS['user']->id;
        $banner_roles = self::getRoles($ad_id);
        $user_roles = RolePersistence::getAssignedRoles($user_id, true);

        if (!$banner_roles) {
            return true;
        }

        foreach ($banner_roles as $banner_role) {
            foreach ($user_roles as $user_role) {
                if ($banner_role->getRoleid() === $user_role->getRoleid()) {
                    return true;
                }
            }
        }

        return false;
    }

    public static function getRoles($ad_id)
    {
        $banner_roles = self::findByad_id($ad_id);
        $banner_role_ids = [];
        foreach ($banner_roles as $banner_role) {
            $banner_role_ids[] = $banner_role['roleid'];
        }

        $only_system_roles = Config::get()->BANNER_ONLY_SYSTEM_ROLES;
        $roles = RolePersistence::getAllRoles();
        $re = [];
        foreach ($banner_role_ids as $role_id) {
            if (isset($roles[$role_id])) {
                if ($only_system_roles && !$roles[$role_id]->getSystemtype()) {
                    continue;
                }
                $re[$role_id] = $roles[$role_id];
            }
        }
        return $re;
    }

    public static function getAvailableRoles($ad_id = null)
    {
        $banner_role_ids = [];
        if ($ad_id) {
            $banner_roles = self::findByad_id($ad_id);
            foreach ($banner_roles as $banner_role) {
                $banner_role_ids[] = $banner_role['roleid'];
            }
        }

        $only_system_roles = Config::get()->BANNER_ONLY_SYSTEM_ROLES;
        $roles = RolePersistence::getAllRoles();
        $rolesStats = RolePersistence::getStatistics();
        $re = [];
        foreach ($roles as $key => $role) {
            if (!in_array($key, $banner_role_ids)) {
                if ($only_system_roles && !$role->getSystemtype()) {
                    continue;
                }
                if ($rolesStats[$role->getRoleid()]['explicit'] + $rolesStats[$role->getRoleid()]['implicit'] == 0) {
                    continue;
                }
                $re[$key] = $role;
            }
        }

        return $re;
    }

    public static function update($ad_id, $new_roles)
    {
        self::deleteByAd_id($ad_id);

        if ($new_roles) {
            foreach ($new_roles as $new_role) {
                $BannerRoles = new self();
                $BannerRoles->ad_id = $ad_id;
                $BannerRoles->roleid = $new_role;
                $BannerRoles->store();
            }
        }
    }
}