1
0
mirror of https://github.com/moparisthebest/mail synced 2024-10-31 15:25:01 -04:00
mail/src/js/controller/login-set-credentials.js

121 lines
4.1 KiB
JavaScript
Raw Normal View History

2014-10-02 16:05:44 -04:00
'use strict';
var ENCRYPTION_METHOD_NONE = 0;
var ENCRYPTION_METHOD_STARTTLS = 1;
var ENCRYPTION_METHOD_TLS = 2;
2014-11-10 12:36:26 -05:00
var appCtrl = require('../app-controller');
2014-10-02 16:05:44 -04:00
var SetCredentialsCtrl = function($scope, $location, $routeParams) {
if (!appCtrl._auth && !$routeParams.dev) {
$location.path('/'); // init app
return;
}
var auth = appCtrl._auth;
var doctor = appCtrl._doctor;
//
// Presets and Settings
//
var provider = $location.search().provider;
$scope.hasProviderPreset = ($scope.state.login && $scope.state.login.mailConfig);
2014-10-02 16:05:44 -04:00
$scope.useOAuth = !!auth.oauthToken;
$scope.showDetails = (provider === 'custom');
// set email address
2014-10-02 16:05:44 -04:00
if ($scope.useOAuth) {
$scope.emailAddress = auth.emailAddress;
} else if ($scope.state.login) {
$scope.emailAddress = $scope.state.login.emailAddress;
2014-10-02 16:05:44 -04:00
}
if ($scope.hasProviderPreset) {
// use editable presets
var mailConfig = $scope.state.login.mailConfig;
2014-10-02 16:05:44 -04:00
// SMTP config
$scope.smtpHost = mailConfig.smtp.hostname;
$scope.smtpPort = parseInt(mailConfig.smtp.port, 10);
// $scope.smtpCert = config[provider].smtp.ca;
// $scope.smtpPinned = config[provider].smtp.pinned;
2014-10-02 16:05:44 -04:00
// transport encryption method
if (mailConfig.smtp.secure && !mailConfig.smtp.ignoreTLS) {
2014-10-02 16:05:44 -04:00
$scope.smtpEncryption = ENCRYPTION_METHOD_TLS;
} else if (!mailConfig.smtp.secure && !mailConfig.smtp.ignoreTLS) {
2014-10-02 16:05:44 -04:00
$scope.smtpEncryption = ENCRYPTION_METHOD_STARTTLS;
} else {
$scope.smtpEncryption = ENCRYPTION_METHOD_NONE;
}
2014-10-02 16:05:44 -04:00
// IMAP config
$scope.imapHost = mailConfig.imap.hostname;
$scope.imapPort = parseInt(mailConfig.imap.port, 10);
// $scope.imapCert = config[provider].imap.ca;
// $scope.imapPinned = config[provider].imap.pinned;
2014-10-02 16:05:44 -04:00
// transport encryption method
if (mailConfig.imap.secure && !mailConfig.imap.ignoreTLS) {
2014-10-02 16:05:44 -04:00
$scope.imapEncryption = ENCRYPTION_METHOD_TLS;
} else if (!mailConfig.imap.secure && !mailConfig.imap.ignoreTLS) {
2014-10-02 16:05:44 -04:00
$scope.imapEncryption = ENCRYPTION_METHOD_STARTTLS;
} else {
$scope.imapEncryption = ENCRYPTION_METHOD_NONE;
}
2014-10-02 16:05:44 -04:00
}
$scope.test = function() {
// parse the <select> dropdown lists
var imapEncryption = parseInt($scope.imapEncryption, 10);
var smtpEncryption = parseInt($scope.smtpEncryption, 10);
// build credentials object
var credentials = {
provider: provider,
emailAddress: $scope.emailAddress,
username: $scope.username || $scope.emailAddress,
realname: $scope.realname,
password: $scope.password,
xoauth2: auth.oauthToken,
imap: {
host: $scope.imapHost.toLowerCase(),
port: $scope.imapPort,
secure: imapEncryption === ENCRYPTION_METHOD_TLS,
ignoreTLS: imapEncryption === ENCRYPTION_METHOD_NONE,
ca: $scope.imapCert,
pinned: !!$scope.imapPinned
},
smtp: {
host: $scope.smtpHost.toLowerCase(),
port: $scope.smtpPort,
secure: smtpEncryption === ENCRYPTION_METHOD_TLS,
ignoreTLS: smtpEncryption === ENCRYPTION_METHOD_NONE,
ca: $scope.smtpCert,
pinned: !!$scope.smtpPinned
}
2014-10-02 16:05:44 -04:00
};
2014-10-02 16:05:44 -04:00
// use the credentials in the connection doctor
doctor.configure(credentials);
// run connection doctor test suite
$scope.busy = true;
doctor.check(function(err) {
if (err) {
// display the error in the settings UI
$scope.connectionError = err;
} else {
2014-10-02 16:05:44 -04:00
// persists the credentials and forwards to /login
auth.setCredentials(credentials);
$location.path('/login');
}
2014-10-02 16:05:44 -04:00
$scope.busy = false;
$scope.$apply();
});
};
2014-10-02 16:05:44 -04:00
};
2014-10-08 06:34:34 -04:00
module.exports = SetCredentialsCtrl;