new route auth

stable
Juanfran 2015-05-13 08:20:40 +02:00
parent 1e92388cdc
commit 4e41ea5015
3 changed files with 31 additions and 80 deletions

View File

@ -41,11 +41,13 @@ configure = ($routeProvider, $locationProvider, $httpProvider, $provide, $tgEven
$routeProvider.when("/",
{
templateUrl: "home/home-page.html",
access: {
requiresLogin: true
},
resolve: {
loader: tgLoaderProvider.add(true),
pageParams: -> {
"title": "PROJECT.WELCOME"
"authRequired": true
}
}
}
@ -54,11 +56,13 @@ configure = ($routeProvider, $locationProvider, $httpProvider, $provide, $tgEven
$routeProvider.when("/projects/",
{
templateUrl: "projects/projects-page.html",
access: {
requiresLogin: true
},
resolve: {
loader: tgLoaderProvider.add(true),
pageParams: -> {
"title": "PROJECT.SECTION_PROJECTS"
"authRequired": true
}
},
controller: "Page"
@ -68,13 +72,12 @@ configure = ($routeProvider, $locationProvider, $httpProvider, $provide, $tgEven
$routeProvider.when("/project/:pslug/",
{
templateUrl: "projects/project/project-page.html",
resolve: {
loader: tgLoaderProvider.add(true),
pageParams: -> {
"authRequired": true
}
access: {
requiresLogin: true
},
controller: "Page"
resolve: {
loader: tgLoaderProvider.add(true)
}
}
)
@ -172,7 +175,17 @@ configure = ($routeProvider, $locationProvider, $httpProvider, $provide, $tgEven
# User profile
$routeProvider.when("/profile",
{templateUrl: "profile/profile.html"})
{
templateUrl: "profile/profile-page.html",
resolve: {
loader: tgLoaderProvider.add(true)
},
access: {
requiresLogin: true
}
controller: "ProfilePage"
}
)
# Auth
$routeProvider.when("/login",
@ -341,7 +354,7 @@ i18nInit = (lang, $translate) ->
checksley.updateMessages('default', messages)
init = ($log, $config, $rootscope, $auth, $events, $analytics, $translate) ->
init = ($log, $rootscope, $auth, $events, $analytics, $translate, $location, $navUrls) ->
$log.debug("Initialize application")
# Taiga Plugins
@ -360,6 +373,10 @@ init = ($log, $config, $rootscope, $auth, $events, $analytics, $translate) ->
# Analytics
$analytics.initialize()
$rootscope.$on '$routeChangeStart', (event, next) ->
if next.access && next.access.requiresLogin
if !$auth.isAuthenticated()
$location.path($navUrls.resolve("login"))
modules = [
# Main Global Modules
@ -426,11 +443,12 @@ module.config([
module.run([
"$log",
"$tgConfig",
"$rootScope",
"$tgAuth",
"$tgEvents",
"$tgAnalytics",
"$translate"
"$translate",
"$tgLocation",
"$tgNavUrls",
init
])

View File

@ -1,17 +1,11 @@
class PageController extends taiga.Controller
@.$inject = [
"$tgAuth",
"$appTitle",
"$translate",
"$tgLocation",
"$tgNavUrls",
"pageParams"
]
constructor: (@auth, @appTitle, @translate, @location, @navUrls, @pageParams) ->
if @pageParams.authRequired && !@auth.isAuthenticated()
@location.path(@navUrls.resolve("login"))
constructor: (@appTitle, @translate, @pageParams) ->
if @pageParams.title
@translate(@pageParams.title).then (text) => @appTitle.set(text)

View File

@ -9,13 +9,6 @@ describe "PageController", ->
provide.value "pageParams", mocks.pageParams
_mockAuth = () ->
mocks.auth = {
isAuthenticated: sinon.stub()
}
provide.value "$tgAuth", mocks.auth
_mockAppTitle = () ->
mocks.appTitle = {
set: sinon.spy()
@ -23,20 +16,6 @@ describe "PageController", ->
provide.value "$appTitle", mocks.appTitle
_mockLocation = () ->
mocks.location = {
path: sinon.spy()
}
provide.value "$tgLocation", mocks.location
_mockNavUrls = () ->
mocks.navUrls = {
resolve: sinon.stub()
}
provide.value "$tgNavUrls", mocks.navUrls
_mockTranslate = () ->
mocks.translate = sinon.stub()
@ -47,9 +26,6 @@ describe "PageController", ->
provide = $provide
_mockAppTitle()
_mockPageParams()
_mockAuth()
_mockLocation()
_mockNavUrls()
_mockTranslate()
return null
@ -62,43 +38,6 @@ describe "PageController", ->
inject ($controller) ->
controller = $controller
describe "auth", () ->
it "if auth is required and the user is not logged redirect to login page", () ->
locationPath = "location-path"
mocks.pageParams.authRequired = true
mocks.auth.isAuthenticated.returns(false)
mocks.navUrls.resolve.withArgs("login").returns(locationPath)
pageCtrl = controller "Page",
$scope: {}
expect(mocks.location.path.withArgs(locationPath)).have.been.calledOnce
it "if auth is not required no redirect to login page", () ->
locationPath = "location-path"
mocks.pageParams.authRequired = false
mocks.auth.isAuthenticated.returns(false)
mocks.navUrls.resolve.withArgs("login").returns(locationPath)
pageCtrl = controller "Page",
$scope: {}
expect(mocks.location.path).have.callCount(0)
it "if auth is required and the user is logged no redirect", () ->
locationPath = "location-path"
mocks.pageParams.authRequired = true
mocks.auth.isAuthenticated.returns(true)
mocks.navUrls.resolve.withArgs("login").returns(locationPath)
pageCtrl = controller "Page",
$scope: {}
expect(mocks.location.path).have.callCount(0)
describe "page title", () ->
it "if title is defined set it", () ->
thenStub = sinon.stub()