Skip to content

Commit 4257b83

Browse files
committed
feat: add sensitive Declarative setting example
Signed-off-by: Andrey Borysenko <[email protected]>
1 parent 193606b commit 4257b83

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

lib/main.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,15 @@ async def lifespan(app: FastAPI):
183183
default="foo",
184184
options={_("First radio"): "foo", _("Second radio"): "bar", _("Third radio"): "baz"},
185185
),
186+
SettingsField(
187+
id="test_ex_app_sensitive_field",
188+
title=_("Password"),
189+
description=_("Set some secure value setting with encryption"),
190+
type=SettingsFieldType.PASSWORD,
191+
default="",
192+
placeholder=_("Set secure value"),
193+
sensitive=True,
194+
),
186195
],
187196
)
188197

@@ -194,7 +203,10 @@ def enabled_handler(enabled: bool, nc: NextcloudApp) -> str:
194203
"top_menu",
195204
"first_menu",
196205
"ui_example_state",
197-
{"initial_value": "test init value"},
206+
{
207+
"initial_value": "test init value",
208+
"initial_sensitive_value": "test_sensitive_value",
209+
},
198210
)
199211
nc.ui.resources.set_script("top_menu", "first_menu", "js/ui_example-main")
200212
nc.ui.top_menu.register("first_menu", "UI example", "img/app.svg")
@@ -234,6 +246,8 @@ def enabled_handler(enabled: bool, nc: NextcloudApp) -> str:
234246
],
235247
)
236248

249+
nc.appconfig_ex.set_value("test_ex_app_sensitive_field", "test_sensitive_value", sensitive=True)
250+
237251
if nc.srv_version["major"] >= 29:
238252
nc.ui.settings.register_form(SETTINGS_EXAMPLE)
239253
else:
@@ -247,13 +261,18 @@ def enabled_handler(enabled: bool, nc: NextcloudApp) -> str:
247261
nc.occ_commands.unregister("ui_example:ping")
248262
nc.occ_commands.unregister("ui_example:setup")
249263
nc.occ_commands.unregister("ui_example:stream")
264+
nc.appconfig_ex.delete("test_ex_app_sensitive_field")
250265
return ""
251266

252267

253268
class Button1Format(BaseModel):
254269
initial_value: str
255270

256271

272+
class Button2Format(BaseModel):
273+
sensitive_value: str
274+
275+
257276
@APP.post("/api/verify_initial_value")
258277
async def verify_initial_value(
259278
input1: Button1Format,
@@ -263,6 +282,16 @@ async def verify_initial_value(
263282
content={"initial_value": str(random.randint(0, 100))}, status_code=200
264283
)
265284

285+
@APP.post("/api/verify_sensitive_value")
286+
async def verify_sensitive_value(
287+
input1: Button2Format,
288+
nc: Annotated[NextcloudApp, Depends(nc_app)],
289+
):
290+
print("Old sensitive value: ", input1.sensitive_value)
291+
sensitive_value = nc.appconfig_ex.get_value("test_ex_app_sensitive_field")
292+
print("Sensitive value: ", sensitive_value)
293+
return responses.JSONResponse(content={"sensitive_value": sensitive_value}, status_code=200)
294+
266295

267296
@APP.post("/api/test_menu")
268297
async def test_menu_handler(

src/store/example.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,22 @@ const actions = {
3737
})
3838
},
3939

40+
verifySensitiveValue(context, value) {
41+
axios.post(generateUrl(`${APP_API_PROXY_URL_PREFIX}/${EX_APP_ID}/api/verify_sensitive_value`), {
42+
sensitive_value: value,
43+
})
44+
.then((res) => {
45+
if ('sensitive_value' in res.data && res.data.sensitive_value === value) {
46+
showSuccess(t('ui_example', 'Sensitive value is correct'))
47+
} else {
48+
showError(t('ui_example', 'Sensitive value is incorrect'))
49+
}
50+
})
51+
.catch(() => {
52+
showError(t('ui_example', 'Sensitive value is incorrect'))
53+
})
54+
},
55+
4056
sendNextcloudFileToExApp(context, fileInfo) {
4157
axios.post(generateUrl(`${APP_API_PROXY_URL_PREFIX}/${EX_APP_ID}/api/nextcloud_file`), {
4258
file_info: fileInfo,

src/views/ExAppView.vue

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@
1212
<NcButton @click="verifyInitialValue">
1313
{{ t('ui_example', 'Verify initial value') }}
1414
</NcButton>
15+
16+
<div style="margin: 10px 0; display: flex; align-items: center; width: 100%; justify-content: center; flex-direction: column;">
17+
<NcInputField :value.sync="initialState.initial_sensitive_value"
18+
:label="t('ui_example', 'Test sensitive value')" />
19+
<NcButton style="margin: 10px 0;" @click="verifySensitiveValue">
20+
{{ t('ui_example', 'Verify sensitive value') }}
21+
</NcButton>
22+
</div>
1523
</div>
1624
</NcAppContent>
1725
</NcContent>
@@ -48,6 +56,9 @@ export default {
4856
verifyInitialValue() {
4957
this.$store.dispatch('verifyInitialStateValue', this.initialState?.initial_value)
5058
},
59+
verifySensitiveValue() {
60+
this.$store.dispatch('verifySensitiveValue', this.initialState?.initial_sensitive_value)
61+
},
5162
},
5263
}
5364
</script>

0 commit comments

Comments
 (0)