Passed
Push — master ( 262b3f...86674d )
by Yannick
09:44
created

OnlyofficeConfigService::getCustomization()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 14
rs 9.9666
1
<?php
2
/**
3
 * (c) Copyright Ascensio System SIA 2025.
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
use Onlyoffice\DocsIntegrationSdk\Models\Customization;
18
use Onlyoffice\DocsIntegrationSdk\Models\EditorsMode;
19
use Onlyoffice\DocsIntegrationSdk\Models\GoBack;
20
use Onlyoffice\DocsIntegrationSdk\Models\Permissions;
21
use Onlyoffice\DocsIntegrationSdk\Models\User;
22
use Onlyoffice\DocsIntegrationSdk\Service\DocEditorConfig\DocEditorConfigService;
23
24
class OnlyofficeConfigService extends DocEditorConfigService
25
{
26
    public function __construct($settingsManager, $jwtManager, $documentManager)
27
    {
28
        parent::__construct($settingsManager, $jwtManager, $documentManager);
29
    }
30
31
    public function getEditorsMode()
32
    {
33
        if ($this->isEditable() && $this->getAccessRights() && !$this->isReadOnly()) {
34
            $editorsMode = new EditorsMode('edit');
35
        } else {
36
            if ($this->canView()) {
37
                $editorsMode = new EditorsMode('view');
38
            } else {
39
                api_not_allowed(true);
40
            }
41
        }
42
43
        return $editorsMode;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $editorsMode does not seem to be defined for all execution paths leading up to this point.
Loading history...
44
    }
45
46
    public function isEditable()
47
    {
48
        return $this->documentManager->isDocumentEditable($this->documentManager->getDocInfo('title'));
49
    }
50
51
    public function canView()
52
    {
53
        return $this->documentManager->isDocumentViewable($this->documentManager->getDocInfo('title'));
54
    }
55
56
    public function getAccessRights()
57
    {
58
        $isAllowToEdit = api_is_allowed_to_edit(true, true);
59
        $isMyDir = DocumentManager::is_my_shared_folder(
0 ignored issues
show
Bug introduced by
The method is_my_shared_folder() does not exist on DocumentManager. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

59
        /** @scrutinizer ignore-call */ 
60
        $isMyDir = DocumentManager::is_my_shared_folder(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
60
            api_get_user_id(),
61
            $this->documentManager->getDocInfo('absolute_parent_path'),
62
            api_get_session_id()
63
        );
64
        $isGroupAccess = false;
65
        if (!empty($this->documentManager->getGroupId())) {
66
            $groupProperties = GroupManager::get_group_properties($this->documentManager->getGroupId());
67
            $docInfoGroup = api_get_item_property_info(
0 ignored issues
show
Bug introduced by
The function api_get_item_property_info was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

67
            $docInfoGroup = /** @scrutinizer ignore-call */ api_get_item_property_info(
Loading history...
68
                api_get_course_int_id(),
69
                'document',
70
                $docId,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $docId seems to be never defined.
Loading history...
71
                $sessionId
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $sessionId seems to be never defined.
Loading history...
72
            );
73
            $isGroupAccess = GroupManager::allowUploadEditDocument(
74
                $userId,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $userId seems to be never defined.
Loading history...
75
                $courseCode,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $courseCode seems to be never defined.
Loading history...
76
                $groupProperties,
77
                $docInfoGroup
78
            );
79
            $isMemberGroup = GroupManager::is_user_in_group($userId, $groupProperties);
0 ignored issues
show
Bug introduced by
The method is_user_in_group() does not exist on GroupManager. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

79
            /** @scrutinizer ignore-call */ 
80
            $isMemberGroup = GroupManager::is_user_in_group($userId, $groupProperties);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
80
            if (!$isGroupAccess) {
81
                if (!$groupProperties['status']) {
82
                    api_not_allowed(true);
83
                }
84
                if (!$isMemberGroup && 1 != $groupProperties['doc_state']) {
85
                    api_not_allowed(true);
86
                }
87
            }
88
        }
89
90
        // Allow editing if the document is part of an exercise
91
        if (!empty($_GET['exerciseId']) || !empty($_GET['exeId'])) {
92
            return true;
93
        }
94
95
        $accessRights = $isAllowToEdit || $isMyDir || $isGroupAccess;
96
97
        return $accessRights;
98
    }
99
100
    public function isReadOnly()
101
    {
102
        return $this->documentManager->getDocInfo('readonly');
103
    }
104
105
    public function getUser()
106
    {
107
        $user = new User();
108
        $user->setId(api_get_user_id());
109
        $userInfo = api_get_user_info($userId);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $userId does not exist. Did you maybe mean $user?
Loading history...
110
        $user->setName($userInfo['username']);
111
112
        return $user;
113
    }
114
115
    public function getCustomization(string $fileId)
116
    {
117
        $goback = new GoBack();
118
119
        if (!empty($this->documentManager->getGobackUrl($fileId))) {
120
            $goback->setUrl($this->documentManager->getGobackUrl($fileId));
121
        }
122
        $goback->setBlank(false);
123
        $customization = new Customization();
124
        $customization->setGoback($goback);
125
        $customization->setCompactHeader(true);
126
        $customization->setToolbarNoTabs(true);
127
128
        return $customization;
129
    }
130
131
    public function getLang()
132
    {
133
        return $this->getLangInfo();
134
    }
135
136
    public function getRegion()
137
    {
138
        return $this->getLangInfo();
139
    }
140
141
    public function getLangInfo()
142
    {
143
        $langInfo = LangManager::getLangUser();
144
145
        return $langInfo['isocode'];
146
    }
147
148
    public function getPermissions(string $fileId = '')
149
    {
150
        $permsEdit = $this->getAccessRights() && !$this->isReadOnly();
151
        $isFillable = $this->documentManager->isDocumentFillable($this->documentManager->getDocInfo('title'));
152
153
        $permissions = new Permissions(null,
154
            null,
155
            null,
156
            null,
157
            null,
158
            null,
159
            $permsEdit,
160
            null,
161
            $isFillable,
162
            null,
163
            null,
164
            null,
165
            null,
166
            null,
167
            null,
168
            null,
169
            null
170
        );
171
172
        return $permissions;
173
    }
174
175
    public function getCoEditing(string $fileId = '', $mode = null, $type)
176
    {
177
        return null;
178
    }
179
}
180