Skip to content

Commit 78c4c0e

Browse files
committed
fix(user_ldap): Improve typing
Signed-off-by: Louis Chmn <[email protected]>
1 parent 05f5fe6 commit 78c4c0e

File tree

5 files changed

+28
-19
lines changed

5 files changed

+28
-19
lines changed

apps/user_ldap/src/components/SettingsTabs/GroupsTab.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,10 @@ const ldapGroupFilterGroups = computed({
9696
*/
9797
async function init() {
9898
const response1 = await callWizard('determineGroupObjectClasses', props.configId)
99-
groupObjectClasses.value = response1.options!.ldap_groupfilter_objectclass
99+
groupObjectClasses.value = response1.options?.ldap_groupfilter_objectclass ?? []
100100
101101
const response2 = await callWizard('determineGroupsForGroups', props.configId)
102-
groupGroups.value = response2.options!.ldap_groupfilter_groups
102+
groupGroups.value = response2.options?.ldap_groupfilter_groups ?? []
103103
}
104104
105105
init()
@@ -110,7 +110,7 @@ init()
110110
async function getGroupFilter() {
111111
const response = await callWizard('getGroupFilter', props.configId)
112112
// Not using ldapConfig to avoid triggering the save logic.
113-
ldapConfigs.value[props.configId].ldapGroupFilter = response.changes!.ldap_group_filter as string
113+
ldapConfigs.value[props.configId]!.ldapGroupFilter = (response.changes?.ldap_group_filter as string | undefined) ?? ''
114114
}
115115
116116
/**

apps/user_ldap/src/components/SettingsTabs/LoginTab.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ const filteredLoginFilterOptions = computed(() => loginFilterOptions.value.filte
9090
*/
9191
async function init() {
9292
const response = await callWizard('determineAttributes', props.configId)
93-
loginFilterOptions.value = response.options!.ldap_loginfilter_attributes
93+
loginFilterOptions.value = response.options?.ldap_loginfilter_attributes ?? []
9494
}
9595
9696
init()
@@ -102,7 +102,7 @@ async function getUserLoginFilter() {
102102
if (ldapConfigProxy.value.ldapLoginFilterMode === '0') {
103103
const response = await callWizard('getUserLoginFilter', props.configId)
104104
// Not using ldapConfig to avoid triggering the save logic.
105-
ldapConfigs.value[props.configId].ldapLoginFilter = response.changes!.ldap_login_filter as string
105+
ldapConfigs.value[props.configId]!.ldapLoginFilter = (response.changes?.ldap_login_filter as string | undefined) ?? ''
106106
}
107107
}
108108

apps/user_ldap/src/components/SettingsTabs/UsersTab.vue

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,14 @@ const ldapUserFilterGroups = computed({
9797
*/
9898
async function init() {
9999
const response1 = await callWizard('determineUserObjectClasses', props.configId)
100-
userObjectClasses.value = response1.options!.ldap_userfilter_objectclass
100+
userObjectClasses.value = response1.options?.ldap_userfilter_objectclass ?? []
101101
// Not using ldapConfig to avoid triggering the save logic.
102-
ldapConfigs.value[props.configId].ldapUserFilterObjectclass = response1.changes!.ldap_userfilter_objectclass?.join(';') ?? ''
102+
ldapConfigs.value[props.configId]!.ldapUserFilterObjectclass = (response1.changes?.ldap_userfilter_objectclass as string[] | undefined)?.join(';') ?? ''
103103
104104
const response2 = await callWizard('determineGroupsForUsers', props.configId)
105-
userGroups.value = response2.options!.ldap_userfilter_groups
105+
userGroups.value = response2.options?.ldap_userfilter_groups ?? []
106106
// Not using ldapConfig to avoid triggering the save logic.
107-
ldapConfigs.value[props.configId].ldapUserFilterGroups = response2.changes!.ldap_userfilter_groups?.join(';') ?? ''
107+
ldapConfigs.value[props.configId]!.ldapUserFilterGroups = (response2.changes?.ldap_userfilter_groups as string[] | undefined)?.join(';') ?? ''
108108
}
109109
110110
init()
@@ -116,11 +116,11 @@ async function reloadFilters() {
116116
if (ldapConfigProxy.value.ldapUserFilterMode === '0') {
117117
const response1 = await callWizard('getUserListFilter', props.configId)
118118
// Not using ldapConfig to avoid triggering the save logic.
119-
ldapConfigs.value[props.configId].ldapUserFilter = response1.changes!.ldap_userlist_filter as string
119+
ldapConfigs.value[props.configId]!.ldapUserFilter = (response1.changes?.ldap_userlist_filter as string | undefined) ?? ''
120120
121121
const response2 = await callWizard('getUserLoginFilter', props.configId)
122122
// Not using ldapConfig to avoid triggering the save logic.
123-
ldapConfigs.value[props.configId].ldapLoginFilter = response2.changes!.ldap_userlogin_filter as string
123+
ldapConfigs.value[props.configId]!.ldapLoginFilter = (response2.changes?.ldap_userlogin_filter as string | undefined) ?? ''
124124
}
125125
}
126126

apps/user_ldap/src/services/ldapConfigService.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export async function getConfig(configId: string): Promise<LDAPConfig> {
7474
* @param configId
7575
* @param config
7676
*/
77-
export async function updateConfig(configId: string, config: LDAPConfig): Promise<LDAPConfig> {
77+
export async function updateConfig(configId: string, config: Partial<LDAPConfig>): Promise<LDAPConfig> {
7878
const response = await axios.put(
7979
generateOcsUrl('apps/user_ldap/api/v1/config/{configId}', { configId }),
8080
{ configData: config },
@@ -142,7 +142,7 @@ export async function clearMapping(subject: 'user' | 'group') {
142142
{ subject },
143143
) as AxiosResponse<OCSResponse>
144144

145-
logger.debug('Cleared mapping', { subject, params, response })
145+
logger.debug('Cleared mapping', { subject, response })
146146
showSuccess(t('user_ldap', 'Mapping cleared'))
147147
return true
148148
} catch (error) {

apps/user_ldap/src/store/configs.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import type { LDAPConfig } from '../models/index.ts'
77

88
import { loadState } from '@nextcloud/initial-state'
99
import { defineStore } from 'pinia'
10-
import Vue, { computed, ref } from 'vue'
10+
import { computed, ref } from 'vue'
1111
import { copyConfig, createConfig, deleteConfig, getConfig, updateConfig } from '../services/ldapConfigService.ts'
1212

1313
export const useLDAPConfigsStore = defineStore('ldap-configs', () => {
1414
const ldapConfigs = ref(loadState('user_ldap', 'ldapConfigs') as Record<string, LDAPConfig>)
15-
const selectedConfigId = ref<string>(Object.keys(ldapConfigs.value)[0])
16-
const selectedConfig = computed(() => ldapConfigs.value[selectedConfigId.value])
15+
const selectedConfigId = ref<string | undefined>(Object.keys(ldapConfigs.value)[0])
16+
const selectedConfig = computed(() => selectedConfigId.value === undefined ? undefined : ldapConfigs.value[selectedConfigId.value])
1717
const updatingConfig = ref(0)
1818

1919
/**
@@ -22,6 +22,10 @@ export const useLDAPConfigsStore = defineStore('ldap-configs', () => {
2222
* @param postSetHooks
2323
*/
2424
function getConfigProxy<J>(configId: string, postSetHooks: Partial<Record<keyof LDAPConfig, (value: J) => void>> = {}) {
25+
if (ldapConfigs.value[configId] === undefined) {
26+
throw new Error(`Config with id ${configId} does not exist`)
27+
}
28+
2529
return new Proxy(ldapConfigs.value[configId], {
2630
get(target, property) {
2731
return target[property]
@@ -49,7 +53,7 @@ export const useLDAPConfigsStore = defineStore('ldap-configs', () => {
4953
*/
5054
async function create() {
5155
const configId = await createConfig()
52-
Vue.set(ldapConfigs.value, configId, await getConfig(configId))
56+
ldapConfigs.value[configId] = await getConfig(configId)
5357
selectedConfigId.value = configId
5458
return configId
5559
}
@@ -59,8 +63,13 @@ export const useLDAPConfigsStore = defineStore('ldap-configs', () => {
5963
* @param fromConfigId
6064
*/
6165
async function _copyConfig(fromConfigId: string) {
66+
if (ldapConfigs.value[fromConfigId] === undefined) {
67+
throw new Error(`Config with id ${fromConfigId} does not exist`)
68+
}
69+
6270
const configId = await copyConfig(fromConfigId)
63-
Vue.set(ldapConfigs.value, configId, { ...ldapConfigs.value[fromConfigId] })
71+
72+
ldapConfigs.value[configId] = { ...ldapConfigs.value[fromConfigId] }
6473
selectedConfigId.value = configId
6574
return configId
6675
}
@@ -72,7 +81,7 @@ export const useLDAPConfigsStore = defineStore('ldap-configs', () => {
7281
async function removeConfig(configId: string) {
7382
const result = await deleteConfig(configId)
7483
if (result === true) {
75-
Vue.delete(ldapConfigs.value, configId)
84+
delete ldapConfigs.value[configId]
7685
}
7786

7887
selectedConfigId.value = Object.keys(ldapConfigs.value)[0] ?? await create()

0 commit comments

Comments
 (0)