Commit 02e5a3b9 authored by Gergely Ujvari's avatar Gergely Ujvari

Introduce user helper

parent 2cee0faa
class AccountController class AccountController
@inject = [ '$rootScope', '$scope', '$filter', @inject = [ '$scope', '$filter',
'flash', 'session', 'identity', 'formHelpers'] 'flash', 'formHelpers', 'identity', 'session', 'user']
constructor: ($rootScope, $scope, $filter, constructor: ($scope, $filter,
flash, session, identity, formHelpers) -> flash, formHelpers, identity, session, user ) ->
persona_filter = $filter('persona') persona_filter = $filter('persona')
$scope.subscriptionDescription = $scope.subscriptionDescription =
reply: 'Receive notification emails when: - Someone replies to one of my annotations' reply: 'Receive notification emails when: - Someone replies to one of my annotations'
...@@ -33,7 +33,7 @@ class AccountController ...@@ -33,7 +33,7 @@ class AccountController
$scope.$broadcast 'formState', form.$name, '' # Update status btn $scope.$broadcast 'formState', form.$name, '' # Update status btn
$scope.tab = 'Account' $scope.tab = 'Account'
session.profile({user_id: $rootScope.persona}).$promise session.profile({user_id: user.getPersona()}).$promise
.then (result) => .then (result) =>
$scope.subscriptions = result.subscriptions $scope.subscriptions = result.subscriptions
...@@ -47,7 +47,7 @@ class AccountController ...@@ -47,7 +47,7 @@ class AccountController
# The extension is then removed from the page. # The extension is then removed from the page.
# Confirmation of success is given. # Confirmation of success is given.
return unless form.$valid return unless form.$valid
username = persona_filter $rootScope.persona username = persona_filter user.getPersona()
packet = packet =
username: username username: username
pwd: form.pwd.$modelValue pwd: form.pwd.$modelValue
...@@ -62,7 +62,7 @@ class AccountController ...@@ -62,7 +62,7 @@ class AccountController
formHelpers.applyValidationErrors(form) formHelpers.applyValidationErrors(form)
return unless form.$valid return unless form.$valid
username = persona_filter $rootScope.persona username = persona_filter user.getPersona()
packet = packet =
username: username username: username
pwd: form.pwd.$modelValue pwd: form.pwd.$modelValue
...@@ -77,7 +77,7 @@ class AccountController ...@@ -77,7 +77,7 @@ class AccountController
$scope.updated = (index, form) -> $scope.updated = (index, form) ->
packet = packet =
username: $rootScope.persona username: user.getPersona()
subscriptions: JSON.stringify $scope.subscriptions[index] subscriptions: JSON.stringify $scope.subscriptions[index]
successHandler = angular.bind(null, onSuccess, form) successHandler = angular.bind(null, onSuccess, form)
......
# User authorization function for the Permissions plugin.
authorizeAction = (action, annotation, user) ->
if annotation.permissions
tokens = annotation.permissions[action] || []
if tokens.length == 0
# Empty or missing tokens array: only admin can perform action.
return false
for token in tokens
if user == token
return true
if token == 'group:__world__'
return true
# No tokens matched: action should not be performed.
return false
# Coarse-grained authorization
else if annotation.user
return user and user == annotation.user
# No authorization info on annotation: free-for-all!
true
class AppController class AppController
this.$inject = [ this.$inject = [
'$location', '$route', '$scope', '$timeout', '$location', '$route', '$scope', '$timeout',
'annotator', 'flash', 'identity', 'streamer', 'streamfilter', 'annotator', 'flash', 'identity', 'streamer', 'streamfilter',
'documentHelpers', 'drafts' 'documentHelpers', 'drafts', 'user'
] ]
constructor: ( constructor: (
$location, $route, $scope, $timeout, $location, $route, $scope, $timeout,
annotator, flash, identity, streamer, streamfilter, annotator, flash, identity, streamer, streamfilter,
documentHelpers, drafts documentHelpers, drafts, user
) -> ) ->
{plugins, host, providers} = annotator {plugins, host, providers} = annotator
checkingToken = false
isFirstRun = $location.search().hasOwnProperty('firstrun') isFirstRun = $location.search().hasOwnProperty('firstrun')
applyUpdates = (action, data) -> applyUpdates = (action, data) ->
...@@ -81,7 +54,7 @@ class AppController ...@@ -81,7 +54,7 @@ class AppController
Store = plugins.Store Store = plugins.Store
delete plugins.Store delete plugins.Store
if $rootScope.persona or annotator.socialView.name is 'none' if user.getPersona() or annotator.socialView.name is 'none'
annotator.addPlugin 'Store', annotator.options.Store annotator.addPlugin 'Store', annotator.options.Store
$scope.store = plugins.Store $scope.store = plugins.Store
...@@ -106,12 +79,12 @@ class AppController ...@@ -106,12 +79,12 @@ class AppController
Store.updateAnnotation = angular.noop Store.updateAnnotation = angular.noop
# Sort out which annotations should remain in place. # Sort out which annotations should remain in place.
user = $rootScope.persona persona = user.getPersona()
view = annotator.socialView.name view = annotator.socialView.name
cull = (acc, annotation) -> cull = (acc, annotation) ->
if view is 'single-player' and annotation.user != user if view is 'single-player' and annotation.user != persona
acc.drop.push annotation acc.drop.push annotation
else if authorizeAction 'read', annotation, user else if authorizeAction 'read', annotation, persona
acc.keep.push annotation acc.keep.push annotation
else else
acc.drop.push annotation acc.drop.push annotation
...@@ -133,41 +106,18 @@ class AppController ...@@ -133,41 +106,18 @@ class AppController
$timeout -> cleanup rest $timeout -> cleanup rest
onlogin = (assertion) -> onlogin = (assertion) ->
checkingToken = true user.login assertion, reset
# Configure the Auth plugin with the issued assertion as refresh token.
annotator.addPlugin 'Auth',
tokenUrl: documentHelpers.absoluteURI(
"/api/token?assertion=#{assertion}")
# Set the user from the token.
plugins.Auth.withToken (token) ->
checkingToken = false
annotator.addPlugin 'Permissions',
user: token.userId
userAuthorize: authorizeAction
$scope.$apply ->
$rootScope.persona = token.userId
reset()
onlogout = -> onlogout = ->
plugins.Auth?.element.removeData('annotator:headers') user.logout()
plugins.Auth?.destroy()
delete plugins.Auth
plugins.Permissions?.setUser(null)
plugins.Permissions?.destroy()
delete plugins.Permissions
$rootScope.persona = null
checkingToken = false
reset() reset()
onready = -> onready = ->
if not checkingToken and typeof $rootScope.persona == 'undefined' persona = user.getPersona()
if not user.checkingInProgress() and typeof persona == 'undefined'
# If we're not checking the token and persona is undefined, onlogin # If we're not checking the token and persona is undefined, onlogin
# hasn't run, which means we aren't authenticated. # hasn't run, which means we aren't authenticated.
$rootScope.persona = null user.noPersona()
reset() reset()
if isFirstRun if isFirstRun
...@@ -177,6 +127,7 @@ class AppController ...@@ -177,6 +127,7 @@ class AppController
$scope.dialog.visible = false $scope.dialog.visible = false
reset = -> reset = ->
$scope.persona = user.getPersona()
$scope.dialog.visible = false $scope.dialog.visible = false
# Update any edits in progress. # Update any edits in progress.
...@@ -193,7 +144,7 @@ class AppController ...@@ -193,7 +144,7 @@ class AppController
$scope.$watch 'socialView.name', (newValue, oldValue) -> $scope.$watch 'socialView.name', (newValue, oldValue) ->
return if newValue is oldValue return if newValue is oldValue
initStore() initStore()
if newValue is 'single-player' and not $rootScope.persona if newValue is 'single-player' and not user.getPersona()
annotator.show() annotator.show()
flash 'info', flash 'info',
'You will need to sign in for your highlights to be saved.' 'You will need to sign in for your highlights to be saved.'
......
...@@ -37,10 +37,10 @@ validate = (value) -> ...@@ -37,10 +37,10 @@ validate = (value) ->
# {@link annotator annotator service} for persistence. # {@link annotator annotator service} for persistence.
### ###
AnnotationController = [ AnnotationController = [
'$rootScope', '$scope', '$timeout', '$scope', '$timeout',
'annotator', 'drafts', 'flash', 'documentHelpers', 'timeHelpers', 'annotator', 'drafts', 'flash', 'documentHelpers', 'timeHelpers', 'user'
($rootScope, $scope, $timeout, ($scope, $timeout,
annotator, drafts, flash, documentHelpers, timeHelpers annotator, drafts, flash, documentHelpers, timeHelpers, user
) -> ) ->
@annotation = {} @annotation = {}
@action = 'view' @action = 'view'
...@@ -182,16 +182,17 @@ AnnotationController = [ ...@@ -182,16 +182,17 @@ AnnotationController = [
reply = {references, uri} reply = {references, uri}
annotator.publish 'beforeAnnotationCreated', reply annotator.publish 'beforeAnnotationCreated', reply
if $rootScope.persona? persona = user.getPersona()
reply.permissions.update = [$rootScope.persona] if persona?
reply.permissions.delete = [$rootScope.persona] reply.permissions.update = [persona]
reply.permissions.admin = [$rootScope.persona] reply.permissions.delete = [persona]
reply.permissions.admin = [persona]
# If replying to a public annotation make the response public. # If replying to a public annotation make the response public.
if 'group:__world__' in (model.permissions.read or []) if 'group:__world__' in (model.permissions.read or [])
reply.permissions.read = ['group:__world__'] reply.permissions.read = ['group:__world__']
else else
reply.permissions.read = [$rootScope.persona] reply.permissions.read = [persona]
###* ###*
# @ngdoc method # @ngdoc method
......
# User authorization function for the Permissions plugin.
authorizeAction = (action, annotation, user) ->
if annotation.permissions
tokens = annotation.permissions[action] || []
if tokens.length == 0
# Empty or missing tokens array: only admin can perform action.
return false
for token in tokens
if user == token
return true
if token == 'group:__world__'
return true
# No tokens matched: action should not be performed.
return false
# Coarse-grained authorization
else if annotation.user
return user and user == annotation.user
# No authorization info on annotation: free-for-all!
true
class User
_persona: undefined
_checkingToken: false
login: undefined
logout: undefined
this.$inject = ['annotator', 'documentHelpers']
constructor: ( annotator, documentHelpers) ->
{plugins} = annotator
@login = (assertion, callbackFn) ->
@_checkingToken = true
# Configure the Auth plugin with the issued assertion as refresh token.
annotator.addPlugin 'Auth',
tokenUrl: documentHelpers.absoluteURI(
"/api/token?assertion=#{assertion}")
# Set the user from the token.
plugins.Auth.withToken (token) =>
@_checkingToken = false
annotator.addPlugin 'Permissions',
user: token.userId
userAuthorize: authorizeAction
@_persona = token.userId
callbackFn()
@logout = ->
plugins.Auth?.element.removeData('annotator:headers')
plugins.Auth?.destroy()
delete plugins.Auth
plugins.Permissions?.setUser(null)
plugins.Permissions?.destroy()
delete plugins.Permissions
@_persona = null
@_checkingToken = false
checkingInProgress: -> @_checkingToken
getPersona: -> @_persona
noPersona: -> @_persona = null
angular.module('h')
.service('user', User)
\ No newline at end of file
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