Permission.class.php 5.5 KB
Newer Older
Игорь's avatar
init    
Игорь committed
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
123
124
125
126
127
128
129
130
131
132
133
<?php

/**
 * Permission
 * 
 * This class has been auto-generated by the Doctrine ORM Framework
 * 
 * @package    sf
 * @subpackage model
 * @author     Atma
 * @version    SVN: $Id: Builder.php 7490 2010-03-29 19:53:27Z jwage $
 */
class Permission extends PluginPermission
{
    public static $base_permission = array(
        'index|filter|search' => 'Просмотр списка',
        'show|edit' => 'Просмотр элемента',
        'new|create' => 'Добавление',
        'update' => 'Редактирование',
        'delete|batchDelete' => 'Удаление',
        'batch|batchMerge' => 'Объединение'
    );
    public static function build($module = false)
    {
        $actions = Permission::$base_permission;

        $actions_stop = array('select_list');
        foreach ($actions as $ak => $av) {
            $actions_stop = array_merge($actions_stop, explode('|', $ak));
        }

        $exist = array();
        $new = array();

        $permissions = Doctrine_Query::create()
            ->select("p.*")
            ->from('Permission p')
            ->execute();

        foreach ($permissions as $permission) {
            $exist[] = $permission['id'];
        }

        $parser = new sfYamlParser();
        $modules = glob(sfConfig::get('sf_app_module_dir') . '/*');
        $links = array();
        foreach ($modules as $k => $v) {
            if($module && $module != basename($v)){
                continue;
            }
            if (basename($v) == 'user_group' || basename($v) == 'permission' || basename($v) == 'page' || basename($v) == 'push_log' || basename($v) == 'user_log') {
                continue;
            }
            if (file_exists($v . '/config/generator.yml')) {
                $p = $parser->parse(file_get_contents($v . '/config/generator.yml'));
                foreach ($actions as $ak => $av) {
                    if ($ak == 'batch|batchMerge' && !isset($p['generator']['param']['config']['list']['batch_actions']['_merge'])) {
                        continue;
                    }

                    $perm = Doctrine::getTable('Permission')->findOneByCredential(basename($v) . '-' . $ak);
                    if (!$perm) {
                        $perm = new Permission();
                        $perm->setCredential(basename($v) . '-' . $ak);
                    }
                    $perm->setDescription(strtolower($p['generator']['param']['config']['list']['title'] . ':' . $av));
                    $perm->save();
                    $new[] = $perm->getId();
                }

                $more_actions = array();

                if (file_exists($v . '/actions/actions.class.php')) {
                    $lines = file($v . '/actions/actions.class.php');

                    foreach ($lines as $line_k => $line) {
                        $line = trim($line);
                        $actionpos = strpos($line, 'public function execute');
                        if ($actionpos !== false) {
                            $name = substr($line, 23, strpos($line, '(') - 23);
                            $name = strtolower(preg_replace(array('/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'), '\\1_\\2', $name));
                            if (!in_array($name, $actions_stop)) {
                                $title = $name;
                                $titleline = trim($lines[$line_k - 1]);
                                $titlelinepos = strpos($titleline, '// @title');
                                if ($titlelinepos !== false) {
                                    $titletmp = substr($titleline, 9);
                                    if ($titletmp != '') {
                                        $title = trim($titletmp);
                                    }
                                }
                                $more_actions[strtolower($name)] = $title;
                            }
                        }
                    }
                }
                foreach ($more_actions as $ak => $av) {
                    $perm = Doctrine::getTable('Permission')->findOneByCredential(basename($v) . '-' . $ak);
                    if (!$perm) {
                        $perm = new Permission();
                        $perm->setCredential(basename($v) . '-' . $ak);
                    }
                    $perm->setDescription(strtolower($p['generator']['param']['config']['list']['title'] . ':' . $av));
                    $perm->save();
                    $new[] = $perm->getId();
                }
            }
        }
        if (count($new) > 0 && !$module) {
            $diff = array_diff($exist, $new);
            if (count($diff) > 0) {
                $permissions = Doctrine_Query::create()
                    ->select("p.*")
                    ->from('Permission p')
                    ->whereIn("p.id", $diff)
                    ->execute();
                foreach ($permissions as $permission) {
                    $user_permissions = Doctrine_Query::create()
                        ->delete("up.*")
                        ->from('UserPermissions up')
                        ->where("up.permission_id = ?", $permission['id'])
                        ->execute();
                    $user_group_permissions = Doctrine_Query::create()
                        ->delete("ugp.*")
                        ->from('UserGroupPermissions ugp')
                        ->where("ugp.permission_id = ?", $permission['id'])
                        ->execute();
                    $permission->delete();
                }
            }
        }
    }
}