aboutsummaryrefslogtreecommitdiffstats
path: root/apps/settings/src/admin.js
blob: 054dd7ced2eac9b874214bce939f1a28f2bfde81 (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
window.addEventListener('DOMContentLoaded', () => {
	$('#loglevel').change(function() {
		$.post(OC.generateUrl('/settings/admin/log/level'), { level: $(this).val() }, () => {
			OC.Log.reload()
		})
	})

	$('#mail_smtpauth').change(function() {
		if (!this.checked) {
			$('#mail_credentials').addClass('hidden')
		} else {
			$('#mail_credentials').removeClass('hidden')
		}
	})

	$('#mail_smtpmode').change(function() {
		if ($(this).val() !== 'smtp') {
			$('#setting_smtpauth').addClass('hidden')
			$('#setting_smtphost').addClass('hidden')
			$('#mail_smtpsecure_label').addClass('hidden')
			$('#mail_smtpsecure').addClass('hidden')
			$('#mail_credentials').addClass('hidden')
			$('#mail_sendmailmode_label, #mail_sendmailmode').removeClass('hidden')
		} else {
			$('#setting_smtpauth').removeClass('hidden')
			$('#setting_smtphost').removeClass('hidden')
			$('#mail_smtpsecure_label').removeClass('hidden')
			$('#mail_smtpsecure').removeClass('hidden')
			if ($('#mail_smtpauth').is(':checked')) {
				$('#mail_credentials').removeClass('hidden')
			}
			$('#mail_sendmailmode_label, #mail_sendmailmode').addClass('hidden')
		}
	})

	const changeEmailSettings = function() {
		if (OC.PasswordConfirmation.requiresPasswordConfirmation()) {
			OC.PasswordConfirmation.requirePasswordConfirmation(changeEmailSettings)
			return
		}

		OC.msg.startSaving('#mail_settings_msg')
		$.ajax({
			url: OC.generateUrl('/settings/admin/mailsettings'),
			type: 'POST',
			data: $('#mail_general_settings_form').serialize(),
			success: () => {
				OC.msg.finishedSuccess('#mail_settings_msg', t('settings', 'Saved'))
			},
			error: (xhr) => {
				OC.msg.finishedError('#mail_settings_msg', xhr.responseJSON)
			},
		})
	}

	const toggleEmailCredentials = function() {
		if (OC.PasswordConfirmation.requiresPasswordConfirmation()) {
			OC.PasswordConfirmation.requirePasswordConfirmation(toggleEmailCredentials)
			return
		}

		OC.msg.startSaving('#mail_settings_msg')
		$.ajax({
			url: OC.generateUrl('/settings/admin/mailsettings/credentials'),
			type: 'POST',
			data: $('#mail_credentials_settings').serialize(),
			success: () => {
				OC.msg.finishedSuccess('#mail_settings_msg', t('settings', 'Saved'))
			},
			error: (xhr) => {
				OC.msg.finishedError('#mail_settings_msg', xhr.responseJSON)
			},
		})
	}

	$('#mail_general_settings_form').change(changeEmailSettings)
	$('#mail_credentials_settings_submit').click(toggleEmailCredentials)
	$('#mail_smtppassword').click(() => {
		if (this.type === 'text' && this.value === '********') {
			this.type = 'password'
			this.value = ''
		}
	})

	$('#sendtestemail').click((event) => {
		event.preventDefault()
		OC.msg.startAction('#sendtestmail_msg', t('settings', 'Sending…'))

		$.ajax({
			url: OC.generateUrl('/settings/admin/mailtest'),
			type: 'POST',
			success: () => {
				OC.msg.finishedSuccess('#sendtestmail_msg', t('settings', 'Email sent'))
			},
			error: (xhr) => {
				OC.msg.finishedError('#sendtestmail_msg', xhr.responseJSON)
			},
		})
	})

	const setupChecks = () => {
		// run setup checks then gather error messages
		$.when(
			OC.SetupChecks.checkSetup(),
		).then((messages) => {
			const $el = $('#postsetupchecks')
			$('#security-warning-state-loading').addClass('hidden')

			let hasMessages = false
			const $errorsEl = $el.find('.errors')
			const $warningsEl = $el.find('.warnings')
			const $infoEl = $el.find('.info')

			for (let i = 0; i < messages.length; i++) {
				switch (messages[i].type) {
				case OC.SetupChecks.MESSAGE_TYPE_INFO:
					$infoEl.append('<li>' + messages[i].msg + '</li>')
					break
				case OC.SetupChecks.MESSAGE_TYPE_WARNING:
					$warningsEl.append('<li>' + messages[i].msg + '</li>')
					break
				case OC.SetupChecks.MESSAGE_TYPE_ERROR:
				default:
					$errorsEl.append('<li>' + messages[i].msg + '</li>')
				}
			}

			if ($errorsEl.find('li').length > 0) {
				$errorsEl.removeClass('hidden')
				hasMessages = true
			}
			if ($warningsEl.find('li').length > 0) {
				$warningsEl.removeClass('hidden')
				hasMessages = true
			}
			if ($infoEl.find('li').length > 0) {
				$infoEl.removeClass('hidden')
				hasMessages = true
			}

			if (hasMessages) {
				$('#postsetupchecks-hint').removeClass('hidden')
				if ($errorsEl.find('li').length > 0) {
					$('#security-warning-state-failure').removeClass('hidden')
				} else {
					$('#security-warning-state-warning').removeClass('hidden')
				}
			} else {
				const securityWarning = $('#security-warning')
				if (securityWarning.children('ul').children().length === 0) {
					$('#security-warning-state-ok').removeClass('hidden')
				} else {
					$('#security-warning-state-failure').removeClass('hidden')
				}
			}
		})
	}

	if (document.getElementById('security-warning') !== null) {
		setupChecks()
	}
})