aboutsummaryrefslogtreecommitdiff
path: root/src/site/store/admin.js
blob: 9b1d591103fcea7dd2897b37c170fb2012fd1c52 (plain) (blame)
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
export const state = () => ({
	users: [],
	user: {
		id: null,
		username: null,
		enabled: false,
		createdAt: null,
		editedAt: null,
		apiKeyEditedAt: null,
		isAdmin: null,
		files: []
	},
	file: {},
	settings: {},
	statistics: {},
	settingsSchema: {
		type: null,
		keys: {}
	}
});

export const actions = {
	async fetchSettings({ commit }) {
		const response = await this.$axios.$get('service/config/all');
		commit('setSettings', response);

		return response;
	},
	async saveSettings({ commit }, settings) {
		const response = await this.$axios.$post('service/config', { settings });

		return response;
	},
	async fetchStatistics({ commit }, category) {
		const url = category ? `service/statistics/${category}` : 'service/statistics';
		const response = await this.$axios.$get(url);
		commit('setStatistics', { statistics: response.statistics, category });

		return response;
	},
	async getSettingsSchema({ commit }) {
		// XXX: Maybe move to the config store?
		const response = await this.$axios.$get('service/config/schema');

		commit('setSettingsSchema', response);
	},
	async fetchUsers({ commit }) {
		const response = await this.$axios.$get('admin/users');
		commit('setUsers', response);

		return response;
	},
	async fetchUser({ commit }, { id, page }) {
		page = page || 1;
		const response = await this.$axios.$get(`admin/users/${id}`, { params: { limit: 50, page } });
		commit('setUserInfo', { ...response, page });

		return response;
	},
	async fetchFile({ commit }, id) {
		const response = await this.$axios.$get(`admin/file/${id}`);
		commit('setFile', response);
		commit('setUserInfo', response);

		return response;
	},
	async banIP(_, ip) {
		const response = await this.$axios.$post('admin/ban/ip', { ip });

		return response;
	},
	async enableUser({ commit }, id) {
		const response = await this.$axios.$post('admin/users/enable', { id });

		commit('changeUserState', { userId: id, enabled: true });

		return response;
	},
	async disableUser({ commit }, id) {
		const response = await this.$axios.$post('admin/users/disable', { id });

		commit('changeUserState', { userId: id, enabled: false });

		return response;
	},
	async promoteUser({ commit }, id) {
		const response = await this.$axios.$post('admin/users/promote', { id });

		commit('changeUserState', { userId: id, isAdmin: true });

		return response;
	},
	async demoteUser({ commit }, id) {
		const response = await this.$axios.$post('admin/users/demote', { id });

		commit('changeUserState', { userId: id, isAdmin: false });

		return response;
	},
	async purgeUserFiles(_, id) {
		const response = await this.$axios.$post('admin/users/purge', { id });

		return response;
	},
	async restartService() {
		const response = await this.$axios.$post('service/restart');

		return response;
	}
};

export const mutations = {
	setStatistics(state, { statistics, category }) {
		if (category) {
			state.statistics[category] = statistics[category];
		} else {
			state.statistics = statistics;
		}
	},
	setSettings(state, { config }) {
		state.settings = config;
	},
	setSettingsSchema(state, { schema }) {
		state.settingsSchema = schema;
	},
	setUsers(state, { users }) {
		state.users = users;
	},
	setUserInfo(state, { user, files, count }) {
		state.user = { ...state.user, ...user };
		state.user.files = files || [];
		state.user.totalFiles = count;
	},
	setFile(state, { file }) {
		state.file = file || {};
	},
	changeUserState(state, { userId, enabled, isAdmin }) {
		const foundIndex = state.users.findIndex(({ id }) => id === userId);
		if (foundIndex > -1) {
			if (enabled !== undefined) {
				state.users[foundIndex].enabled = enabled;
			}
			if (isAdmin !== undefined) {
				state.users[foundIndex].isAdmin = isAdmin;
			}
		}

		if (state.user.id === userId) {
			if (enabled !== undefined) {
				state.user.enabled = enabled;
			}
			if (isAdmin !== undefined) {
				state.user.isAdmin = isAdmin;
			}
		}
	},
	populateSchemaWithValues({ settings, settingsSchema }) {
		for (const [key, value] of Object.entries(settings)) {
			if (settingsSchema.keys?.[key] !== undefined) {
				settingsSchema.keys[key].value = value;
			}
		}
	}
};