aboutsummaryrefslogtreecommitdiffhomepage
path: root/main/app/sprinkles/ConfigManager/src/Database/Migrations/v101/SettingsPermissions.php
blob: c3928d983634cead99fe0ee1f9747a1e922c4c19 (plain) (blame)
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
<?php
/**
 * UF Config Manager
 *
 * @link      https://github.com/lcharette/UF_ConfigManager
 * @copyright Copyright (c) 2016 Louis Charette
 * @license   https://github.com/lcharette/UF_ConfigManager/blob/master/LICENSE (MIT License)
 */
namespace UserFrosting\Sprinkle\ConfigManager\Database\Migrations\v101;

use UserFrosting\System\Bakery\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
use UserFrosting\Sprinkle\Account\Database\Models\Permission;
use UserFrosting\Sprinkle\Account\Database\Models\Role;

/**
 * Settings permissions migration
 * @extends Migration
 */
class SettingsPermissions extends Migration
{
    /**
     * {@inheritDoc}
     */
    public $dependencies = [
        '\UserFrosting\Sprinkle\Account\Database\Migrations\v400\PermissionsTable',
        '\UserFrosting\Sprinkle\ConfigManager\Database\Migrations\v100\SettingsTable'
    ];

    /**
     * {@inheritDoc}
     */
    public function up()
    {
        // Check if permission exist
        $permissionExist = Permission::where('slug', 'update_site_config')->first();
        if ($permissionExist) {
            $this->io->warning("\nPermission slug `update_site_config` already exist. Skipping...");
            return;
        }

        // Add default permissions
        $permission = new Permission([
            'slug' => 'update_site_config',
            'name' => 'Update site configuration',
            'conditions' => 'always()',
            'description' => 'Edit site configuration from the UI'
        ]);
        $permission->save();

        $roleSiteAdmin = Role::where('slug', 'site-admin')->first();
        if ($roleSiteAdmin) {
            $roleSiteAdmin->permissions()->attach([
                $permission->id
            ]);
        }
    }

    /**
     * {@inheritDoc}
     */
    public function down()
    {
        $permissions = Permission::where('slug', 'update_site_config')->get();
        foreach ($permissions as $permission) {
            $permission->delete();
        }
    }
}