Finish the module auth

stable
David Barragán Merino 2014-07-21 16:23:15 +02:00
parent 8a52bf9a73
commit b172b07ed9
8 changed files with 130 additions and 92 deletions

View File

@ -21,8 +21,7 @@
@taiga = taiga = {}
configure = ($routeProvider, $locationProvider, $httpProvider, $provide,
$compileProvider, $gmUrlsProvider) ->
configure = ($routeProvider, $locationProvider, $httpProvider, $provide) ->
$routeProvider.when("/project/:pslug/backlog", {templateUrl: "/partials/backlog.html"})
$routeProvider.when("/project/:pslug/taskboard/:id", {templateUrl: "/partials/taskboard.html"})
@ -87,14 +86,17 @@ configure = ($routeProvider, $locationProvider, $httpProvider, $provide,
$httpProvider.defaults.headers.put = defaultHeaders
$httpProvider.defaults.headers.get = {}
# authHttpIntercept = ($q, $location) ->
# return (promise) ->
# return promise.then null, (response) ->
# if response.status == 401 or response.status == 0
# $location.url("/login?next=#{$location.path()}")
# return $q.reject(response)
# $provide.factory("authHttpIntercept", ["$q", "$location", authHttpIntercept])
# $httpProvider.responseInterceptors.push('authHttpIntercept')
# Add next param when user try to access to a secction need auth permissions.
authHttpIntercept = ($q, $location) ->
return (promise) ->
return promise.then null, (response) ->
if response.status == 401 or response.status == 0
nextPath = $location.path()
$location.url("/login").search("next=#{nextPath}")
return $q.reject(response)
$provide.factory("authHttpIntercept", ["$q", "$location", authHttpIntercept])
$httpProvider.responseInterceptors.push('authHttpIntercept')
init = ($log, $i18n, $config, $rootscope) ->
@ -139,6 +141,7 @@ module.config([
"$routeProvider",
"$locationProvider",
"$httpProvider",
'$provide',
configure
])

View File

@ -31,7 +31,7 @@ class ConfigService extends taiga.Service
"es": "Spanish"
"en": "English"
}
allowPublicRegistration: false
pubblicRegisterEnabled: false
}
initialize: (localconfig) ->

View File

@ -28,7 +28,12 @@ module = angular.module("taigaAuth", ["taigaResources"])
#############################################################################
class AuthService extends taiga.Service
@.$inject = ["$rootScope", "$tgStorage", "$tgModel", "$tgResources", "$tgHttp", "$tgUrls"]
@.$inject = ["$rootScope",
"$tgStorage",
"$tgModel",
"$tgResources",
"$tgHttp",
"$tgUrls"]
constructor: (@rootscope, @storage, @model, @rs, @http, @urls) ->
super()
@ -60,6 +65,9 @@ class AuthService extends taiga.Service
getToken: ->
return @storage.get("token")
removeToken: ->
@storage.remove("token")
isAuthenticated: ->
if @.getUser() != null
return true
@ -75,6 +83,8 @@ class AuthService extends taiga.Service
data = _.clone(data, false)
data.type = if type then type else "normal"
@.removeToken()
return @http.post(url, data).then (data, status) =>
user = @model.make_model("users", data.data)
@.setToken(user.auth_token)
@ -89,23 +99,30 @@ class AuthService extends taiga.Service
if type == "private"
data.existing = if existing then existing else false
@.removeToken()
return @http.post(url, data).then (response) =>
user = @model.make_model("users", response.data)
@.setToken(user.auth_token)
@.setUser(user)
return user
getInvitation: (token) ->
return @rs.invitations.get(token)
acceptInvitiationWithNewUser: (data) ->
return register(data, "private", false)
return @.register(data, "private", false)
acceptInvitiationWithExistingUser: (data) ->
return register(data, "private", true)
return @.register(data, "private", true)
forgotPassword: (data) ->
url = @urls.resolve("users-password-recovery")
data = _.clone(data, false)
@.removeToken()
return @http.post(url, data)
@ -114,10 +131,9 @@ class AuthService extends taiga.Service
data = _.clone(data, false)
return @http.post(url, data)
@.removeToken()
getInvitation: (token) ->
return @rs.invitations.get(token)
return @http.post(url, data)
module.service("$tgAuth", AuthService)
@ -131,26 +147,29 @@ module.service("$tgAuth", AuthService)
## Login Directive
###################
LoginDirective = ($auth, $confirm, $location, $config) ->
LoginDirective = ($auth, $confirm, $location, $config, $routeParams) ->
link = ($scope, $el, $attrs) ->
$scope.allowPublicRegistration = $config.get("allowPublicRegistration")
$scope.pubblicRegisterEnabled = $config.get("pubblicRegisterEnabled")
$scope.data = {}
form = $el.find("form").checksley()
onSuccessSubmit = (response) ->
if $routeParams and $routeParams['next'] and $routeParams['next'] != '/login'
$location.url($routeParams['next'])
else
console.log("TODO: Redirect to '/'") # TODO: Redirect to /
$location.path("/project/project-example-0/backlog")
onErrorSubmit = (response) ->
$confirm.notify("light-error", "According to our Oompa Loompas, your username/email or password
are incorrect.") #TODO: i18n
submit = ->
if not form.validate()
return
promise = $auth.login($scope.data)
promise.then (response) ->
# TODO: finish this. Go tu user home page
$location.path("/project/project-example-0/backlog")
promise.then null, (response) ->
$confirm.notify("light-error", "According to our Oompa Loompas,
your username/email or password
are incorrect.") #TODO: i18n
promise.then(onSuccessSubmit, onErrorSubmit)
$el.on "submit", (event) ->
event.preventDefault()
@ -162,30 +181,33 @@ LoginDirective = ($auth, $confirm, $location, $config) ->
return {link:link}
module.directive("tgLogin", ["$tgAuth", "$tgConfirm", "$location", "$tgConfig", "$routeParams", LoginDirective])
###################
## Register Directive
###################
RegisterDirective = ($auth, $confirm) ->
RegisterDirective = ($auth, $confirm, $location) ->
link = ($scope, $el, $attrs) ->
$scope.data = {}
form = $el.find("form").checksley()
onSuccessSubmit = (response) ->
$confirm.notify("success", "Our Oompa Loompas are happy, wellcome to Taiga.") #TODO: i18n
# TODO: finish this. Go tu '/'
$location.path("/project/project-example-0/backlog")
onErrorSubmit = (response) ->
$confirm.notify("light-error", "According to our Oompa Loompas, the username or email is
already in use.") #TODO: i18n
submit = ->
if not form.validate()
return
promise = $auth.register($scope.data)
promise.then (response) ->
$confirm.notify("success", "Our Oompa Loompas are happy, wellcome to Taiga.") #TODO: i18n
# TODO: finish this. Go tu user home page
$location.path("/project/project-example-0/backlog")
promise.then null, (response) ->
$confirm.notify("light-error", "According to our Oompa Loompas,
your are not registered yet or
type an invalid password.") #TODO: i18n
promise.then(onSuccessSubmit, onErrorSubmit)
$el.on "submit", (event) ->
event.preventDefault()
@ -197,6 +219,8 @@ RegisterDirective = ($auth, $confirm) ->
return {link:link}
module.directive("tgRegister", ["$tgAuth", "$tgConfirm", "$location", RegisterDirective])
###################
## Forgot Password Directive
@ -207,21 +231,23 @@ ForgotPasswordDirective = ($auth, $confirm, $location) ->
$scope.data = {}
form = $el.find("form").checksley()
onSuccessSubmit = (response) ->
$location.path("/login") # TODO: Use the future 'urls' service
$confirm.success("<strong>Check your inbox!</strong><br />
We have sent a mail to<br />
<strong>#{response.data.email}</strong><br />
with the instructions to set a new password") #TODO: i18n
onErrorSubmit = (response) ->
$confirm.notify("light-error", "According to our Oompa Loompas,
your are not registered yet.") #TODO: i18n
submit = ->
if not form.validate()
return
promise = $auth.forgotPassword($scope.data)
promise.then (response) ->
$location.path("/login") # TODO: Use the future 'urls' service
$confirm.success("<strong>Check your inbox!</strong><br />
We have sent a mail to<br />
<strong>#{response.data.email}</strong><br />
with the instructions to set a new password") #TODO: i18n
promise.then null, (response) ->
$confirm.notify("light-error", "According to our Oompa Loompas,
your are not registered yet.") #TODO: i18n
promise.then(onSuccessSubmit, onErrorSubmit)
$el.on "submit", (event) ->
event.preventDefault()
@ -233,6 +259,8 @@ ForgotPasswordDirective = ($auth, $confirm, $location) ->
return {link:link}
module.directive("tgForgotPassword", ["$tgAuth", "$tgConfirm", "$location", ForgotPasswordDirective])
###################
## Change Password from Recovery Directive
@ -250,19 +278,21 @@ ChangePasswordFromRecoveryDirective = ($auth, $confirm, $location, $params) ->
form = $el.find("form").checksley()
onSuccessSubmit = (response) ->
$location.path("/login") # TODO: Use the future 'urls' service
$confirm.success("Our Oompa Loompas save your new password.<br />
Try to <strong>sign in</strong> with it.") #TODO: i18n
onErrorSubmit = (response) ->
$confirm.notify("light-error", "One of our Oompa Loompas say
'#{response.data._error_message}'.") #TODO: i18n
submit = ->
if not form.validate()
return
promise = $auth.changePasswordFromRecovery($scope.data)
promise.then (response) ->
$location.path("/login") # TODO: Use the future 'urls' service
$confirm.success("Our Oompa Loompas save your new password.<br />
Try to <strong>sign in</strong> with it.") #TODO: i18n
promise.then null, (response) ->
$confirm.notify("light-error", "One of our Oompa Loompas say
'#{response.data._error_message}'.") #TODO: i18n
promise.then(onSuccessSubmit, onErrorSubmit)
$el.on "submit", (event) ->
event.preventDefault()
@ -274,6 +304,9 @@ ChangePasswordFromRecoveryDirective = ($auth, $confirm, $location, $params) ->
return {link:link}
module.directive("tgChangePasswordFromRecovery", ["$tgAuth", "$tgConfirm", "$location", "$routeParams",
ChangePasswordFromRecoveryDirective])
###################
## Invitation
@ -284,13 +317,13 @@ InvitationDirective = ($auth, $confirm, $location, $params) ->
token = $params.token
promise = $auth.getInvitation(token)
promise.then (invitation)->
promise.then (invitation) ->
$scope.invitation = invitation
promise.then null, (response) ->
$location.path("/login") # TODO: Use the future 'urls' service
$confirm.success("<strong>Ooops, we have a problems</strong><br />
Our Oompa Loompas can't find your invitations.") #TODO: i18n
$location.path("/login") # TODO: Use the future 'urls' service
$confirm.success("<strong>Ooops, we have a problems</strong><br />
Our Oompa Loompas can't find your invitations.") #TODO: i18n
#$##############
# Login form
@ -298,22 +331,22 @@ InvitationDirective = ($auth, $confirm, $location, $params) ->
$scope.dataLogin = {token: token}
loginForm = $el.find("form.login-form").checksley()
onSuccessSubmitLogin = (response) ->
# TODO: finish this. Go to project home page
$location.path("/project/#{$scope.invitation.project_slug}/backlog")
$confirm.notify("success", "You've successfully joined to this project",
"Wellcome to #{$scope.invitation.project_name}")
onErrorSubmitLogin = (response) ->
$confirm.notify("light-error", "According to our Oompa Loompas, your are not registered yet or
type an invalid password.") #TODO: i18n
submitLogin = ->
if not loginForm.validate()
return
promise = $auth.acceptInvitiationWithExistingUser($scope.dataLogin)
promise.then (response) ->
# TODO: finish this. Go tu project home page
$location.path("/project/#{$scope.invitation.project_slug}/backlog")
$confirm.notify("success", "You've successfully joined to this project",
"Wellcome to #{$scope.invitation.project_name}")
promise.then null, (response) ->
if response.data._error_message
$confirm.notify("light-error", "According to our Oompa Loompas,
your are not registered yet or
type an invalid password.") #TODO: i18n
promise.then(onSuccessSubmitLogin, onErrorSubmitLogin)
$el.on "submit", "form.login-form", (event) ->
event.preventDefault()
@ -329,21 +362,22 @@ InvitationDirective = ($auth, $confirm, $location, $params) ->
$scope.dataRegister = {token: token}
registerForm = $el.find("form.register-form").checksley()
onSuccessSubmitRegister = (response) ->
# TODO: finish this. Go tu project home page
$location.path("/project/#{$scope.invitation.project_slug}/backlog")
$confirm.notify("success", "You've successfully joined to this project",
"Wellcome to #{$scope.invitation.project_name}")
onErrorSubmitRegister = (response) ->
$confirm.notify("light-error", "According to our Oompa Loompas, the
username or email is already in use.") #TODO: i18n
submitRegister = ->
if not registerForm.validate()
return
promise = $auth.acceptInvitiationWithNewUser($scope.dataRegister)
promise.then (response) ->
# TODO: finish this. Go tu project home page
$location.path("/project/#{$scope.invitation.project_slug}/backlog")
$confirm.notify("success", "You've successfully joined to this project",
"Wellcome to #{$scope.invitation.project_name}")
promise.then null, (response) ->
$confirm.notify("light-error", "According to our Oompa Loompas,
your username/email or password
are incorrect.") #TODO: i18n
promise.then(onSuccessSubmitRegister, onErrorSubmitRegister)
$el.on "submit", "form.register-form", (event) ->
event.preventDefault()
@ -355,11 +389,5 @@ InvitationDirective = ($auth, $confirm, $location, $params) ->
return {link:link}
module.directive("tgRegister", ["$tgAuth", "$tgConfirm", RegisterDirective])
module.directive("tgLogin", ["$tgAuth", "$tgConfirm", "$location", "$tgConfig", LoginDirective])
module.directive("tgForgotPassword", ["$tgAuth", "$tgConfirm", "$location", ForgotPasswordDirective])
module.directive("tgChangePasswordFromRecovery", ["$tgAuth", "$tgConfirm", "$location", "$routeParams",
ChangePasswordFromRecoveryDirective])
module.directive("tgInvitation", ["$tgAuth", "$tgConfirm", "$location", "$routeParams",
InvitationDirective])

View File

@ -131,8 +131,8 @@ class ConfirmService extends taiga.Service
@.el.find("p").html(NOTIFICATION_MSG[type].message)
body = angular.element("body")
body.find(".notification-message .notification-light").removeClass('active');
body.find(selector).addClass('active');
body.find(".notification-message .notification-light").removeClass('active')
body.find(selector).addClass('active')
if @.tsem
cancelTimeout(@.tsem)

View File

@ -15,7 +15,8 @@ block content
div.burndown(tg-gm-backlog-graph)
include views/modules/burndown
div.backlog-menu
a.trans-button.move-to-current-sprint(href="", title="Move to Current Sprint", id="move-to-current-sprint")
a.trans-button.move-to-current-sprint(href="", title="Move to Current Sprint",
id="move-to-current-sprint")
span.icon.icon-move
span.text Move to current Sprint
a.trans-button(href="", title="Show Filters", id="show-filters-button")

View File

@ -1,6 +1,6 @@
//- To Aadd a new notification type:
//-
//- div.notification-message.notification-message-< type >
//- div.hidden.notification-message.notification-message-< type >
//- (...)
//- h4 < title >
//- (...)

View File

@ -13,6 +13,6 @@ div.login-form-container(tg-login)
a.button.button-login.button-gray(href="", ng-click="ctrl.submit()", title="Sign in") Sign in
input(type="submit", style="display:none")
p.login-text(ng-if="allowPublicRegistration")
p.login-text(ng-if="pubblicRegisterEnabled")
span Not registered yet?
a(href="", tg-nav="register", title="Register") create your free account here

View File

@ -2,6 +2,12 @@ config = {
host: "localhost:8000"
scheme: "http"
debug: true
pubblicRegisterEnabled: true
defaultLanguage: "en"
languageOptions: {
"es": "Spanish"
"en": "English"
}
}
angular.module("taigaLocalConfig", []).value("localconfig", config)