summaryrefslogtreecommitdiff
path: root/db/migrations/2018_01_01_000005_add_angel_supporter_permissions.php
blob: b9d4cdc2d844109e77b51bd59f9d0ac7e6d2867c (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
<?php

namespace Engelsystem\Migrations;

use Engelsystem\Database\Migration\Migration;

class AddAngelSupporterPermissions extends Migration
{
    /** @var string[] */
    protected $data = [
        '2-Engel',
        'shiftentry_edit_angeltype_supporter',
    ];

    /**
     * Run the migration
     */
    public function up()
    {
        if (!$this->schema->hasTable('GroupPrivileges')) {
            return;
        }

        $db = $this->schema->getConnection();
        if (!empty($db->select($this->getQuery('SELECT *'), $this->data))) {
            return;
        }

        // Add permissions to angels to edit angels if they are angeltype supporters
        $db->insert(
            '
                INSERT IGNORE INTO GroupPrivileges (group_id, privilege_id)
                VALUES ((SELECT UID FROM `Groups` WHERE `name` = ?), (SELECT id FROM `Privileges` WHERE `name` = ?))
            ',
            $this->data
        );
    }

    /**
     * Reverse the migration
     */
    public function down()
    {
        if (!$this->schema->hasTable('GroupPrivileges')) {
            return;
        }

        // Remove permission from angels
        $this->schema->getConnection()->delete(
            $this->getQuery('DELETE'),
            $this->data
        );
    }

    /**
     * @param string $type
     * @return string
     */
    protected function getQuery($type)
    {
        return sprintf('
                %s FROM GroupPrivileges
                WHERE group_id = (SELECT UID FROM `Groups` WHERE `name` = ?)
                AND privilege_id = (SELECT id FROM `Privileges` WHERE `name` = ?)        
        ', $type);
    }
}