Commit 10b64ab7 authored by Randall Leeds's avatar Randall Leeds

Merge pull request #2379 from hypothesis/decaf

Decaffeinate config/accounts.coffee & accounts/*.coffee
parents 37d2c0ac 6015cd55
class AccountController
@inject = [ '$scope', '$filter',
'auth', 'flash', 'formRespond', 'identity', 'session']
constructor: ($scope, $filter,
auth, flash, formRespond, identity, session) ->
persona_filter = $filter('persona')
$scope.subscriptionDescription =
reply: 'Someone replies to one of my annotations'
onSuccess = (form, response) ->
# Fire flash messages.
for type, msgs of response.flash
for m in msgs
flash[type](m)
form.$setPristine()
formModel = form.$name.slice(0, -4)
$scope[formModel] = {} # Reset form fields.
$scope.$broadcast 'formState', form.$name, 'success' # Update status btn
$scope.email = response.email
onDelete = (form, response) ->
identity.logout()
onSuccess(form, response)
onError = (form, response) ->
if response.status >= 400 and response.status < 500
formRespond(form, response.data.errors)
else
if response.data.flash
for own type, msgs of response.data.flash
for m in msgs
flash[type](m)
else
flash.error('Sorry, we were unable to perform your request')
$scope.$broadcast 'formState', form.$name, '' # Update status btn
$scope.tab = 'Account'
session.profile().$promise
.then (result) =>
$scope.subscriptions = result.subscriptions
$scope.email = result.email
# Data for each of the forms
$scope.editProfile = {}
$scope.changePassword = {}
$scope.deleteAccount = {}
$scope.delete = (form) ->
# If the password is correct, the account is deleted.
# The extension is then removed from the page.
# Confirmation of success is given.
return unless form.$valid
username = persona_filter auth.user
packet =
username: username
pwd: form.pwd.$modelValue
successHandler = angular.bind(null, onDelete, form)
errorHandler = angular.bind(null, onError, form)
promise = session.disable_user(packet)
promise.$promise.then(successHandler, errorHandler)
$scope.submit = (form) ->
formRespond(form)
return unless form.$valid
username = persona_filter auth.user
packet =
username: username
pwd: form.pwd.$modelValue
password: form.password.$modelValue
successHandler = angular.bind(null, onSuccess, form)
errorHandler = angular.bind(null, onError, form)
$scope.$broadcast 'formState', form.$name, 'loading' # Update status btn
promise = session.edit_profile(packet)
promise.$promise.then(successHandler, errorHandler)
$scope.changeEmailSubmit = (form) ->
formRespond(form)
return unless form.$valid
username = persona_filter auth.user
packet =
username: username
pwd: form.pwd.$modelValue
email: form.email.$modelValue
emailAgain: form.emailAgain.$modelValue
successHandler = angular.bind(null, onSuccess, form)
errorHandler = angular.bind(null, onError, form)
$scope.$broadcast 'formState', form.$name, 'loading' # Update status btn
promise = session.edit_profile(packet)
promise.$promise.then(successHandler, errorHandler)
$scope.updated = (index, form) ->
packet =
username: auth.user
subscriptions: JSON.stringify $scope.subscriptions[index]
successHandler = angular.bind(null, onSuccess, form)
errorHandler = angular.bind(null, onError, form)
promise = session.edit_profile(packet)
promise.$promise.then(successHandler, errorHandler)
angular.module('h')
.controller('AccountController', AccountController)
var angular = require('angular');
AccountController.inject = [
'$scope', '$filter',
'auth', 'flash', 'formRespond', 'identity', 'session'
];
function AccountController(
$scope, $filter,
auth, flash, formRespond, identity, session
) {
var personaFilter = $filter('persona');
$scope.subscriptionDescription = {
reply: 'Someone replies to one of my annotations'
};
function onSuccess(form, response) {
// Fire flash messages
for (var type in response.flash) {
response.flash[type].map(function (message) {
flash[type](message);
});
}
form.$setPristine();
var formModel = form.$name.slice(0, -4);
// Reset form fields
$scope[formModel] = {};
// Update status button
$scope.$broadcast('formState', form.$name, 'success');
$scope.email = response.email;
};
function onDelete(form, response) {
identity.logout();
onSuccess(form, response);
};
function onError(form, response) {
if (response.status >= 400 && response.status < 500) {
formRespond(form, response.data.errors);
} else {
if (response.data.flash) {
for (type in response.data.flash) {
response.data.flash[type].map(function (message) {
flash[type](message);
});
}
} else {
flash.error('Sorry, we were unable to perform your request');
}
}
// Update status button
$scope.$broadcast('formState', form.$name, '');
};
$scope.tab = 'Account';
session.profile().$promise.then(function(result) {
$scope.subscriptions = result.subscriptions;
$scope.email = result.email;
});
// Data for each of the forms
$scope.editProfile = {};
$scope.changePassword = {};
$scope.deleteAccount = {};
$scope.delete = function(form) {
// If the password is correct, the account is deleted.
// The extension is then removed from the page.
// Confirmation of success is given.
if (!form.$valid) {
return;
}
var username = personaFilter(auth.user);
var packet = {
username: username,
pwd: form.pwd.$modelValue
};
var successHandler = angular.bind(null, onDelete, form);
var errorHandler = angular.bind(null, onError, form);
var promise = session.disable_user(packet).$promise;
return promise.then(successHandler, errorHandler);
};
$scope.submit = function(form) {
formRespond(form);
if (!form.$valid) {
return;
}
var username = personaFilter(auth.user);
var packet = {
username: username,
pwd: form.pwd.$modelValue,
password: form.password.$modelValue
};
var successHandler = angular.bind(null, onSuccess, form);
var errorHandler = angular.bind(null, onError, form);
// Update status button
$scope.$broadcast('formState', form.$name, 'loading');
var promise = session.edit_profile(packet).$promise;
return promise.then(successHandler, errorHandler);
};
$scope.changeEmailSubmit = function(form) {
formRespond(form);
if (!form.$valid) {
return;
}
var username = personaFilter(auth.user);
var packet = {
username: username,
pwd: form.pwd.$modelValue,
email: form.email.$modelValue,
emailAgain: form.emailAgain.$modelValue
};
var successHandler = angular.bind(null, onSuccess, form);
var errorHandler = angular.bind(null, onError, form);
// Update status button
$scope.$broadcast('formState', form.$name, 'loading');
var promise = session.edit_profile(packet).$promise;
return promise.then(successHandler, errorHandler);
};
$scope.updated = function(index, form) {
var packet = {
username: auth.user,
subscriptions: JSON.stringify($scope.subscriptions[index])
};
var successHandler = angular.bind(null, onSuccess, form);
var errorHandler = angular.bind(null, onError, form);
var promise = session.edit_profile(packet).$promise;
return promise.then(successHandler, errorHandler);
};
}
angular.module('h').controller('AccountController', AccountController);
angular = require('angular')
class AuthAppController
this.$inject = ['$location', '$scope', '$timeout', '$window', 'session']
constructor: ( $location, $scope, $timeout, $window, session ) ->
onlogin = ->
$window.location.href = '/stream'
$scope.account = {}
$scope.model = {}
$scope.account.tab = $location.path().split('/')[1]
$scope.$on 'auth', (event, err, data) ->
if data?.userid
$timeout onlogin, 1000
$scope.$watch 'account.tab', (tab, old) ->
unless tab is old then $location.path("/#{tab}")
# TODO: We should be calling identity.beginProvisioning() here in order to
# move toward become a federated BrowserID provider.
session.load (data) ->
if data.userid then onlogin()
class AuthPageController
this.$inject = ['$routeParams', '$scope']
constructor: ( $routeParams, $scope ) ->
$scope.model.code = $routeParams.code
$scope.hasActivationCode = !!$routeParams.code
configure = [
'$httpProvider', '$locationProvider', '$routeProvider'
(
$httpProvider, $locationProvider, $routeProvider
) ->
# Use the Pyramid XSRF header name
$httpProvider.defaults.xsrfHeaderName = 'X-CSRF-Token'
$locationProvider.html5Mode(true)
$routeProvider.when '/login',
controller: 'AuthPageController'
templateUrl: 'auth.html'
$routeProvider.when '/register',
controller: 'AuthPageController'
templateUrl: 'auth.html'
$routeProvider.when '/forgot_password',
controller: 'AuthPageController'
templateUrl: 'auth.html'
$routeProvider.when '/reset_password/:code?',
controller: 'AuthPageController'
templateUrl: 'auth.html'
]
angular.module('h')
.config(configure)
.controller('AuthAppController', AuthAppController)
.controller('AuthPageController', AuthPageController)
require('./account-controller')
require('./auth-controller')
var angular = require('angular');
AuthAppController.$inject = [
'$location', '$scope', '$timeout', '$window',
'session'
];
function AuthAppController(
$location, $scope, $timeout, $window,
session
) {
function onlogin() {
window.location.href = '/stream';
};
$scope.account = {};
$scope.model = {};
$scope.account.tab = $location.path().split('/')[1];
$scope.$on('auth', function(event, err, data) {
if (data != null ? data.userid : void 0) {
$timeout(onlogin, 1000);
}
});
$scope.$watch('account.tab', function(tab, old) {
if (tab !== old) {
$location.path('/' + tab);
}
});
// TODO: We should be calling identity.beginProvisioning() here in order to
// move toward become a federated BrowserID provider.
session.load(function(data) {
if (data.userid) {
onlogin();
}
});
}
AuthPageController.$inject = ['$routeParams', '$scope'];
function AuthPageController( $routeParams, $scope) {
$scope.model.code = $routeParams.code;
$scope.hasActivationCode = !!$routeParams.code;
}
configure.$inject = ['$httpProvider', '$locationProvider', '$routeProvider'];
function configure( $httpProvider, $locationProvider, $routeProvider) {
// Use the Pyramid XSRF header name
$httpProvider.defaults.xsrfHeaderName = 'X-CSRF-Token';
$locationProvider.html5Mode(true);
$routeProvider.when('/login', {
controller: 'AuthPageController',
templateUrl: 'auth.html'
});
$routeProvider.when('/register', {
controller: 'AuthPageController',
templateUrl: 'auth.html'
});
$routeProvider.when('/forgot_password', {
controller: 'AuthPageController',
templateUrl: 'auth.html'
});
$routeProvider.when('/reset_password/:code?', {
controller: 'AuthPageController',
templateUrl: 'auth.html'
});
}
angular.module('h')
.config(configure)
.controller('AuthAppController', AuthAppController)
.controller('AuthPageController', AuthPageController);
require('./account-controller');
require('./auth-controller');
class AuthController
this.$inject = ['$scope', '$timeout', 'flash', 'session', 'formRespond']
constructor: ( $scope, $timeout, flash, session, formRespond ) ->
timeout = null
success = (data) ->
if data.userid
$scope.$emit 'auth', null, data
$scope.account.tab =
switch $scope.account.tab
when 'register' then 'login'
when 'forgot_password' then 'reset_password'
when 'reset_password' then 'login'
else $scope.account.tab
angular.copy {}, $scope.model
$scope.form?.$setPristine()
failure = (form, response) ->
try
{errors, reason} = response.data
catch
reason = "Oops, something went wrong on the server. Please try again
later!"
formRespond(form, errors, reason)
this.submit = (form) ->
formRespond(form)
return unless form.$valid
$scope.$broadcast 'formState', form.$name, 'loading'
session[form.$name] $scope.model, success,
angular.bind(this, failure, form)
.$promise.finally -> $scope.$broadcast 'formState', form.$name, ''
$scope.account ?= tab: 'login'
$scope.model ?= {}
$scope.$on 'auth', do ->
preventCancel = $scope.$on '$destroy', ->
if timeout then $timeout.cancel timeout
$scope.$emit 'auth', 'cancel'
$scope.$watchCollection 'model', (value) ->
# Reset the auth forms after five minutes of inactivity
if timeout
$timeout.cancel timeout
# If the model is not empty, start the timeout
if value and not angular.equals(value, {})
timeout = $timeout ->
angular.copy {}, $scope.model
$scope.form?.$setPristine()
flash.info(
'For your security, the forms have been reset due to inactivity.'
)
, 300000
angular.module('h')
.controller('AuthController', AuthController)
AuthController.$inject = [
'$scope', '$timeout',
'flash', 'session', 'formRespond'
];
function AuthController(
$scope, $timeout,
flash, session, formRespond
) {
var pendingTimeout = null;
function success(data) {
if (data.userid) {
$scope.$emit('auth', null, data);
}
$scope.account.tab = (function() {
switch ($scope.account.tab) {
case 'register':
return 'login';
case 'forgot_password':
return 'reset_password';
case 'reset_password':
return 'login';
default:
return $scope.account.tab;
}
})();
angular.copy({}, $scope.model);
if ($scope.form != null) {
$scope.form.$setPristine()
}
};
function failure(form, response) {
var errors, reason;
try {
errors = response.data.errors;
reason = response.data.reason;
} catch (e) {
reason = 'Oops, something went wrong on the server. ' +
'Please try again later!';
}
return formRespond(form, errors, reason);
};
function timeout() {
angular.copy({}, $scope.model);
if ($scope.form != null) {
$scope.form.$setPristine();
}
flash.info('For your security, ' +
'the forms have been reset due to inactivity.');
}
function cancelTimeout() {
if (pendingTimeout == null) {
return;
}
$timeout.cancel(pendingTimeout);
pendingTimeout = null;
}
this.submit = function submit(form) {
formRespond(form);
if (!form.$valid) {
return;
}
$scope.$broadcast('formState', form.$name, 'loading');
var handler = session[form.$name];
var _failure = angular.bind(this, failure, form);
var res = handler($scope.model, success, _failure);
res.$promise.finally(function() {
return $scope.$broadcast('formState', form.$name, '');
});
};
if ($scope.account == null) {
$scope.account = {
tab: 'login'
};
}
if ($scope.model == null) {
$scope.model = {};
}
// Stop the inactivity timeout when the scope is destroyed.
var removeDestroyHandler = $scope.$on('$destroy', function () {
cancelTimeout(pendingTimeout);
$scope.$emit('auth', 'cancel');
});
// Skip the cancel when destroying the scope after a successful auth.
$scope.$on('auth', removeDestroyHandler);
// Reset the auth forms afterfive minutes of inactivity.
$scope.$watchCollection('model', function(value) {
cancelTimeout(pendingTimeout);
if (value && !angular.equals(value, {})) {
pendingTimeout = $timeout(timeout, 300000);
}
});
}
angular.module('h').controller('AuthController', AuthController);
angular = require('angular')
SESSION_ACTIONS = [
'login'
'logout'
'register'
'forgot_password'
'reset_password'
'edit_profile'
'disable_user'
]
configure = [
'$httpProvider', 'identityProvider', 'sessionProvider'
($httpProvider, identityProvider, sessionProvider) ->
# Pending authentication check
authCheck = null
# Use the Pyramid XSRF header name
$httpProvider.defaults.xsrfHeaderName = 'X-CSRF-Token'
identityProvider.checkAuthentication = [
'$q', 'session',
($q, session) ->
(authCheck = $q.defer()).promise.then do ->
session.load().$promise.then (data) ->
if data.userid then authCheck.resolve data.csrf
else authCheck.reject 'no session'
, -> authCheck.reject 'request failure'
]
identityProvider.forgetAuthentication = [
'$q', 'flash', 'session',
($q, flash, session) ->
session.logout({}).$promise
.then ->
authCheck = $q.defer()
authCheck.reject 'no session'
return null
.catch (err) ->
flash.error('Sign out failed!')
throw err
]
identityProvider.requestAuthentication = [
'$q', '$rootScope',
($q, $rootScope) ->
authCheck.promise.catch ->
(authRequest = $q.defer()).promise.finally do ->
$rootScope.$on 'auth', (event, err, data) ->
if err then authRequest.reject err
else authRequest.resolve data.csrf
]
sessionProvider.actions.load =
method: 'GET'
withCredentials: true
sessionProvider.actions.profile =
method: 'GET'
params:
__formid__: 'profile'
withCredentials: true
for action in SESSION_ACTIONS
sessionProvider.actions[action] =
method: 'POST'
params:
__formid__: action
withCredentials: true
]
angular.module('h')
.value('xsrf', token: null)
.config(configure)
var angular = require('angular');
var SESSION_ACTIONS = [
'login', 'logout', 'register', 'forgot_password',
'reset_password', 'edit_profile', 'disable_user'
];
configure.$inject = ['$httpProvider', 'identityProvider', 'sessionProvider'];
function configure( $httpProvider, identityProvider, sessionProvider) {
// Pending authentication check
var authCheck = null;
// Use the Pyramid XSRF header name
$httpProvider.defaults.xsrfHeaderName = 'X-CSRF-Token';
identityProvider.checkAuthentication = [
'$q', 'session', function($q, session) {
authCheck = $q.defer();
session.load().$promise
.then(function (data) {
if (data.userid) {
authCheck.resolve(data.csrf);
} else {
authCheck.reject('no session');
}
})
.catch(function () {
authCheck.reject('request failure');
});
return authCheck.promise;
}
];
identityProvider.forgetAuthentication = [
'$q', 'flash', 'session', function($q, flash, session) {
return session.logout({}).$promise
.then(function() {
authCheck = $q.defer();
authCheck.reject('no session');
return null;
})
.catch(function(err) {
flash.error('Sign out failed!');
throw err;
});
}
];
identityProvider.requestAuthentication = [
'$q', '$rootScope', function($q, $rootScope) {
return authCheck.promise.catch(function () {
var authRequest = $q.defer();
$rootScope.$on('auth', function(event, err, data) {
if (err) {
return authRequest.reject(err);
} else {
return authRequest.resolve(data.csrf);
}
});
return authRequest.promise;
});
}
];
sessionProvider.actions.load = {
method: 'GET',
withCredentials: true
};
sessionProvider.actions.profile = {
method: 'GET',
params: {
__formid__: 'profile'
},
withCredentials: true
};
for (var i = 0; i < SESSION_ACTIONS.length; i++) {
var action = SESSION_ACTIONS[i];
sessionProvider.actions[action] = {
method: 'POST',
params: {
__formid__: action
},
withCredentials: true
};
}
}
angular.module('h')
.value('xsrf', {token: null})
.config(configure);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment