Welcome to mirror list, hosted at ThFree Co, Russian Federation.

setting.rst « basics « developer_manual - github.com/nextcloud/documentation.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: db40d2d7e06d2d940e03a401781397dad01b1ea4 (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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
========
Settings
========

.. sectionauthor:: Carl Schwan <carl@carlschwan.eu>

Each Nextcloud applications can provide both personal and admin settings. For this
you will need to create a section implementing `IIconSection`. This section will be
used in the setting sidebar to create a new entry.

In our case we will create an admin section class in **<myapp>/lib/Sections/NotesAdmin.php**:

.. code-block:: php

    <?php
    namespace OCA\NotesTutorial\Sections;

    use OCP\IL10N;
    use OCP\IURLGenerator;
    use OCP\Settings\IIconSection;

    class NotesAdmin implements IIconSection {
        private IL10N $l;
        private IURLGenerator $urlGenerator;

        public function __construct(IL10N $l, IURLGenerator $urlGenerator) {
            $this->l = $l;
            $this->urlGenerator = $urlGenerator;
        }

        public function getIcon(): string {
            return $this->urlGenerator->imagePath('core', 'actions/settings-dark.svg');
        }

        public function getID(): string {
            return 'notes';
        }

        public function getName(): string {
            return $this->l->t('Notes tutorial');
        }

        public function getPriority(): int {
            return 98;
        }
    }


The next steps is to fill the new admin section with am admin setting. For that, we create a new class
in *<myapp>/lib/Settings/NotesAdmin.php**.

.. code-block:: php

    <?php
    namespace OCA\NotesTutorial\Settings;

    use OCP\AppFramework\Http\TemplateResponse;
    use OCP\IConfig;
    use OCP\IL10N;
    use OCP\Settings\ISettings;

    class NotesAdmin implements ISettings {
        private IL10N $l;
        private IConfig $config;

        public function __construct(IConfig $config, IL10N $l) {
            $this->config = $config;
            $this->l = $l;
        }

        /**
         * @return TemplateResponse
         */
        public function getForm() {
            $parameters = [
                'mySetting' => $this->config->getSystemValue('my_notes_setting', true),
            ];

            return new TemplateResponse('settings', 'settings/admin', $parameters, '');
        }

        public function getSection() {
            return 'notes'; // Name of the previously created section.
        }

        /**
         * @return int whether the form should be rather on the top or bottom of
         * the admin section. The forms are arranged in ascending order of the
         * priority values. It is required to return a value between 0 and 100.
         *
         * E.g.: 70
         */
        public function getPriority() {
            return 10;
        }
    }

The last missing part is to register both classes inside **<myapp>/appinfo/info.xml**.

.. code-block:: xml

    <settings>
        <admin>OCA\NotesTutorial\Settings\NotesAdmin</admin>
        <admin-section>OCA\NotesTutorial\Sections\NotesAdmin</admin-section>
    </settings>

.. note::

   To register personal sections and settings class use `<personal-section>` and
   `<personal>` instead.

Additionally since Nextcloud 23, groups can be granted authorization to access individual
admin settings (`see admin docs <https://docs.nextcloud.com/server/25/admin_manual/configuration_server/admin_delegation_configuration>`_).
This is a feature that needs to be enabled for each admin setting class.
To do so, the setting class needs to implement `IDelegatedSettings` instead of `ISettings`
and implement two additional methods.

.. TODO ON RELEASE: Update version number above on release

.. code-block:: php

    <?php
    namespace OCA\NotesTutorial\Settings;

    use OCP\AppFramework\Http\TemplateResponse;
    use OCP\IConfig;
    use OCP\IL10N;
    use OCP\Settings\IDelegatedSettings;

    class NotesAdmin implements IDelegatedSettings {

        ...

        public function getName(): ?string {
            // This can also return an empty string in case there is only one setting
            // in the section.
            return $this->l->t('Notes Admin Settings');
        }

        public function getAuthorizedAppConfig(): array {
            return [
                // Allow list of regex that the user can modify with this setting.
                'notes' => ['/notes_.*/', '/my_notes_setting/'],
            ];
        }
    }

Additionally, if your setting class needs to fetch data or send data to some admin-only
controllers, you will need to mark the methods in the controller as accessible by the
setting with annotations.

.. code-block:: php

    <?php
    class NotesSettingsController extends Controller {
        /**
         * Save settings
         * @PasswordConfirmationRequired
         * @AuthorizedAdminSetting(settings=OCA\NotesTutorial\Settings\NotesAdmin)
         */
         public function saveSettings($mySetting) {
             ....
         }
         ...
    }


If you have several classes that implement `IDelegatedSettings` for a function. You must add them in the key "settings" and they must seperate with semi-colons.

.. code-block:: php

    <?php
    class NotesSettingsController extends Controller {
        /**
         * Save settings
         * @PasswordConfirmationRequired
         * @AuthorizedAdminSetting(settings=OCA\NotesTutorial\Settings\NotesAdmin;OCA\NotesTutorial\Settings\NotesSubAdmin)
         */
         public function saveSettings($mySetting) {
             ....
         }
         ...
    }