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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,6 +1,6 @@
//- To Aadd a new notification type: //- To Aadd a new notification type:
//- //-
//- div.notification-message.notification-message-< type > //- div.hidden.notification-message.notification-message-< type >
//- (...) //- (...)
//- h4 < title > //- 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 a.button.button-login.button-gray(href="", ng-click="ctrl.submit()", title="Sign in") Sign in
input(type="submit", style="display:none") input(type="submit", style="display:none")
p.login-text(ng-if="allowPublicRegistration") p.login-text(ng-if="pubblicRegisterEnabled")
span Not registered yet? span Not registered yet?
a(href="", tg-nav="register", title="Register") create your free account here a(href="", tg-nav="register", title="Register") create your free account here

View File

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