Passed
Push — master ( 2eab24...078b67 )
by Nicolaas
02:05
created

DefaultDashboardProvider::addSecurityLinks()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 14
c 2
b 0
f 0
nc 4
nop 0
dl 0
loc 17
rs 9.7998
1
<?php
2
3
namespace Sunnysideup\DashboardWelcomeQuicklinks\Api;
4
5
use SilverStripe\Admin\ModelAdmin;
0 ignored issues
show
Bug introduced by
The type SilverStripe\Admin\ModelAdmin was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use SilverStripe\Assets\File;
7
use SilverStripe\Assets\Image;
8
use SilverStripe\Assets\Folder;
9
use SilverStripe\Assets\Upload;
10
use SilverStripe\CMS\Model\SiteTree;
0 ignored issues
show
Bug introduced by
The type SilverStripe\CMS\Model\SiteTree was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use SilverStripe\Core\ClassInfo;
12
use SilverStripe\Core\Config\Config;
13
use SilverStripe\Core\Config\Configurable;
14
use SilverStripe\Core\Injector\Injector;
15
use SilverStripe\MFA\Report\EnabledMembers;
0 ignored issues
show
Bug introduced by
The type SilverStripe\MFA\Report\EnabledMembers was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use SilverStripe\ORM\DataObject;
17
use SilverStripe\Security\DefaultAdminService;
18
use SilverStripe\Security\Group;
19
use SilverStripe\Security\Member;
20
use SilverStripe\Security\Permission;
21
use SilverStripe\VersionedAdmin\ArchiveAdmin;
0 ignored issues
show
Bug introduced by
The type SilverStripe\VersionedAdmin\ArchiveAdmin was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
use Sunnysideup\DashboardWelcomeQuicklinks\Interfaces\DashboardWelcomeQuickLinksProvider;
23
24
class DefaultDashboardProvider implements DashboardWelcomeQuickLinksProvider
25
{
26
    use Configurable;
27
    protected $links = [];
28
29
    public function provideDashboardWelcomeQuickLinks(): array
30
    {
31
        $this->addPagesLinks();
32
        $this->addFindPages();
33
        $this->addFilesAndImages();
34
        $this->addSiteConfigLinks();
35
        if(Permission::check('ADMIN')) {
36
            $this->addSecurityLinks();
37
        }
38
        $this->addModelAdminLinks();
39
        $this->addMeLinks();
40
        return $this->links;
41
    }
42
43
44
    private static $add_phrase = '+';
0 ignored issues
show
introduced by
The private property $add_phrase is not used, and could be removed.
Loading history...
45
    private static $review_phrase = '☑';
0 ignored issues
show
introduced by
The private property $review_phrase is not used, and could be removed.
Loading history...
46
    private static $edit_phrase = '✎';
0 ignored issues
show
introduced by
The private property $edit_phrase is not used, and could be removed.
Loading history...
47
    private static $more_phrase = '... More';
0 ignored issues
show
introduced by
The private property $more_phrase is not used, and could be removed.
Loading history...
48
    private static $model_admins_to_skip = [
0 ignored issues
show
introduced by
The private property $model_admins_to_skip is not used, and could be removed.
Loading history...
49
        ArchiveAdmin::class,
50
    ];
51
    private static $pages_to_skip = [
0 ignored issues
show
introduced by
The private property $pages_to_skip is not used, and could be removed.
Loading history...
52
53
    ];
54
55
56
    protected function addPagesLinks()
57
    {
58
        $this->addGroup('PAGES', 'Pages', 10);
59
        $this->addLink('PAGES', $this->phrase('add'). ' Page', '/admin/pages/add');
60
        $pagesCount = DataObject::get('Page')->count();
61
        $this->addLink('PAGES', $this->phrase('edit'). ' Pages ('.$pagesCount.')', '/admin/pages');
62
        $pageLastEdited = DataObject::get_one('Page', '', true, 'LastEdited DESC');
63
        if ($pageLastEdited) {
64
            $this->addLink('PAGES', '✎ Last Edited Page: '.$pageLastEdited->Title, $pageLastEdited->CMSEditLink());
65
        }
66
        $this->addLink('PAGES', $this->phrase('review'). ' Page Reports', '/admin/reports');
67
        $lastWeekLink = '/admin/pages?'.'q[LastEditedFrom]='.date('Y-m-d', strtotime('-1 week'));
68
        $this->addLink('PAGES', $this->phrase('review'). ' Recently Modified Pages', $lastWeekLink);
69
    }
70
71
    protected function addFindPages()
72
    {
73
        $pages = [];
74
        $pagesToSkip = (array) $this->Config()->get('pages_to_skip');
75
        foreach (ClassInfo::subclassesFor(SiteTree::class, false) as $className) {
76
            if(in_array($className, $pagesToSkip)) {
77
                continue;
78
            }
79
            $pages[$className] = $className;
80
81
        }
82
        $this->addGroup('PAGEFILTER', 'Page Types ('.count($pages).')', 300);
83
        $count = 0;
84
        foreach($pages as $pageClassName) {
85
            $pageCount = $pageClassName::get()->filter(['ClassName' => $pageClassName])->count();
86
            if($pageCount < 1) {
87
                continue;
88
            }
89
            $count++;
90
            if($count > 7) {
91
                $this->addLink('PAGEFILTER', $this->phrase('more'), '/admin/pages');
92
                break;
93
            }
94
            if($pageCount === 1) {
95
                $obj = DataObject::get_one($pageClassName, ['ClassName' => $pageClassName]);
96
                $this->addLink('PAGEFILTER', $this->phrase('edit'). ' '.$pageClassName::singleton()->i18n_singular_name(), $obj->CMSEditLink());
97
                continue;
98
            }
99
            $page = Injector::inst()->get($pageClassName);
100
            $pageTitle = $page->i18n_singular_name();
101
            $query = 'q[ClassName]='.$pageClassName;
102
            $link = 'admin/pages?' . $query;
103
            $this->addLink('PAGEFILTER', $this->phrase('edit'). ' '.$pageTitle.' ('.$pageCount.')', $link);
104
        }
105
    }
106
    protected function addFilesAndImages()
107
    {
108
        // 'Files ('.$filesCount.') and Images ('.$imageCount.')'
109
        $this->addGroup('FILESANDIMAGES', 'Files and Images', 20);
110
        $uploadFolderName = Config::inst()->get(Upload::class, 'uploads_folder');
111
        $uploadFolder = Folder::find_or_make($uploadFolderName);
112
        // all
113
        $this->addLink('FILESANDIMAGES', $this->phrase('edit'). ' Open File Browswer', '/admin/assets');
114
        // per type
115
        $filesCount = File::get()->exclude(['ClassName' => [Folder::class, Image::class]])->count();
116
        $imageCount = File::get()->filter(['ClassName' => [Image::class]])->count();
117
        $this->addLink('FILESANDIMAGES', $this->phrase('review'). ' Images ('.$imageCount.')', 'admin/assets?filter[appCategory]=IMAGE');
118
        $this->addLink('FILESANDIMAGES', $this->phrase('review'). ' Files ('.$filesCount.')', 'admin/assets?filter[appCategory]=DOCUMENT');
119
120
        // default upload folder
121
        $this->addLink('FILESANDIMAGES', $this->phrase('review'). ' Open Default Upload Folder', $uploadFolder->CMSEditLink());
122
123
        // recent
124
        $lastWeekLink = '/admin/assets?'.'filter[lastEditedFrom]='.date('Y-m-d', strtotime('-1 week')).'&view=tile';
125
        $this->addLink('FILESANDIMAGES', $this->phrase('review'). ' Recently modified Files', $lastWeekLink);
126
    }
127
128
    protected function addSiteConfigLinks()
129
    {
130
        $this->addGroup('SITECONFIG', 'Site Wide Configuration', 20);
131
        $this->addLink('SITECONFIG', $this->phrase('review'). ' Site Settings', '/admin/settings');
132
    }
133
134
    protected function addSecurityLinks()
135
    {
136
        $this->addGroup('SECURITY', 'Security', 30);
137
        $this->addLink('SECURITY', $this->phrase('add'). ' User', '/admin/security/users/EditForm/field/users/item/new');
138
        $userCount = Member::get()->count();
139
        $groupCount = Group::get()->count();
140
        $this->addLink('SECURITY', $this->phrase('review'). ' Users ('.$userCount.')', '/admin/security');
141
        $this->addLink('SECURITY', $this->phrase('review'). ' Groups  ('.$groupCount.')', '/admin/security/groups');
142
        DefaultAdminService::singleton()->extend('addSecurityLinks', $this);
143
        $adminGroup = Permission::get_groups_by_permission('ADMIN')->first();
144
        if($adminGroup) {
145
            $userCount = $adminGroup->Members()->count();
146
            $this->addLink('SECURITY', $this->phrase('review'). ' Administrators ('.$userCount.')', '/admin/security/groups/EditForm/field/groups/item/'.$adminGroup->ID.'/edit');
147
        }
148
        if(class_exists(EnabledMembers::class)) {
149
            $obj = Injector::inst()->get(EnabledMembers::class);
150
            $this->addLink('SECURITY', $this->phrase('review'). ' Multi-Factor Authentication Status', $obj->getLink());
151
152
        }
153
    }
154
155
156
157
158
    protected function addModelAdminLinks()
159
    {
160
        $modelAdmins = [];
161
        $skips = (array) $this->Config()->get('model_admins_to_skip');
162
        foreach (ClassInfo::subclassesFor(ModelAdmin::class, false) as $className) {
163
            if(in_array($className, $skips)) {
164
                continue;
165
            }
166
            $modelAdmins[$className] = $className;
167
168
        }
169
        foreach($modelAdmins as $modelAdminClassName) {
170
            $groupAdded = false;
171
            $ma = Injector::inst()->get($modelAdminClassName);
172
            if($ma->canView()) {
173
                $mas = $ma->getManagedModels();
174
                if(count($mas)) {
175
                    $numberOfModels = count($mas);
176
                    $groupCode = strtoupper($modelAdminClassName);
177
                    $count = 0;
178
                    foreach($mas as $model => $title) {
179
                        $count++;
180
                        if($count > 7) {
181
                            $this->addLink('PAGEFILTER', $this->phrase('more'), $ma->Link());
182
                            break;
183
                        }
184
                        if(is_array($title)) {
185
                            $title = $title['title'];
186
                            $model = $title['dataClass'] ?? $model;
187
                        }
188
                        if(! class_exists($model)) {
189
                            continue;
190
                        }
191
                        $obj = Injector::inst()->get($model);
192
                        if($obj && $obj->canView()) {
193
                            if(! $groupAdded) {
194
                                $this->addGroup($groupCode, $ma->menu_title(), 100);
195
                                $groupAdded = true;
196
                            }
197
                            $objectCount = $model::get()->filter(['ClassName' => $model])->count();
198
                            if($objectCount === 1) {
199
                                $obj = DataObject::get_one($model, ['ClassName' => $model]);
200
                                $this->addLink('PAGEFILTER', $this->phrase('edit'). ' '.$model::singleton()->i18n_singular_name(), $obj->CMSEditLink());
201
                                continue;
202
                            }
203
204
                            $link = '';
205
                            if($obj->hasMethod('CMSListLink')) {
206
                                $link = $obj->CMSListLink();
207
                            } else {
208
                                $link = $ma->getLinkForModelTab($model);
209
                            }
210
                            $titleContainsObjectCount = strpos($title, ' ('.$objectCount.')');
211
                            if($titleContainsObjectCount === false) {
212
                                $title .= ' ('.$objectCount.')';
213
                            }
214
                            $this->addLink($groupCode, $this->phrase('edit'). ' '.$title, $link);
215
                            if($numberOfModels < 4) {
216
                                $obj = Injector::inst()->get($model);
217
                                if($obj->canCreate()) {
218
                                    $classNameEscaped = str_replace('\\', '-', $model);
219
                                    $linkNew = $link .= '/EditForm/field/'.$classNameEscaped.'/item/new';
220
                                    $this->addLink($groupCode, $this->phrase('add'). ' '.$obj->i18n_singular_name(), $linkNew);
221
                                }
222
                            }
223
                        }
224
                    }
225
                }
226
            }
227
        }
228
    }
229
230
231
232
    protected function addMeLinks()
233
    {
234
        $this->addGroup('ME', 'My Account', 200);
235
        $this->addLink('ME', $this->phrase('edit') . '  My Details (there is just one of you!)', '/admin/myprofile');
236
        $this->addLink('ME', $this->phrase('review') . '  Test Password Reset', 'Security/lostpassword');
237
        $this->addLink('ME', $this->phrase('review') . '  Log-out', '/Security/logout');
238
    }
239
240
241
    protected function addGroup(string $groupCode, string $title, $sort)
242
    {
243
        $this->links[$groupCode] = [
244
            'Title' => $title,
245
            'SortOrdre' => $sort,
246
        ];
247
    }
248
249
    protected function addLink($groupCode, $title, $link)
250
    {
251
        $this->links[$groupCode]['Items'][] = [
252
            'Title' => $title,
253
            'Link' => $link,
254
        ];
255
    }
256
257
    protected function phrase(string $phrase): string
258
    {
259
        $phrase = $this->config()->get($phrase .'_phrase');
260
        return _t('DashboardWelcomeQuicklinks.'.$phrase, $phrase);
261
    }
262
263
}
264