Commit 3e0b3e16 authored by Gergely Ujvari's avatar Gergely Ujvari Committed by ujvari

Introduce permissions service

parent af0e036b
......@@ -38,9 +38,11 @@ validate = (value) ->
###
AnnotationController = [
'$scope', '$timeout',
'annotator', 'auth', 'drafts', 'flash', 'documentHelpers', 'timeHelpers'
'annotator', 'auth', 'drafts', 'flash', 'documentHelpers', 'permissions',
'timeHelpers'
($scope, $timeout,
annotator, auth, drafts, flash, documentHelpers, timeHelpers
annotator, auth, drafts, flash, documentHelpers, permissions,
timeHelpers
) ->
@annotation = {}
@action = 'view'
......@@ -183,15 +185,10 @@ AnnotationController = [
annotator.publish 'beforeAnnotationCreated', reply
if auth.user?
reply.permissions.update = [auth.user]
reply.permissions.delete = [auth.user]
reply.permissions.admin = [auth.user]
# If replying to a public annotation make the response public.
if 'group:__world__' in (model.permissions.read or [])
reply.permissions.read = ['group:__world__']
if permissions.isPublic model
reply.permissions = permissions.public()
else
reply.permissions.read = [auth.user]
reply.permissions = permissions.private()
###*
# @ngdoc method
......@@ -279,10 +276,7 @@ AnnotationController = [
# Save highlights once logged in.
if highlight and this.isHighlight()
if model.user
model.permissions.read = [model.user]
model.permissions.update = [model.user]
model.permissions.delete = [model.user]
model.permissions.admin = [model.user]
model.permissions = permissions.private()
annotator.publish 'annotationCreated', model
highlight = false # skip this on future updates
else
......
privacy = ['$window', ($window) ->
privacy = ['$window', 'permissions', ($window, permissions) ->
VISIBILITY_KEY ='hypothesis.visibility'
VISIBILITY_PUBLIC = 'public'
VISIBILITY_PRIVATE = 'private'
......@@ -37,10 +37,10 @@ privacy = ['$window', ($window) ->
link: (scope, elem, attrs, controller) ->
return unless controller?
controller.$formatters.push (permissions) ->
return unless permissions?
controller.$formatters.push (selectedPermissions) ->
return unless selectedPermissions?
if 'group:__world__' in (permissions.read or [])
if permissions.isPublic {permissions: selectedPermissions}
getLevel(VISIBILITY_PUBLIC)
else
getLevel(VISIBILITY_PRIVATE)
......@@ -48,18 +48,17 @@ privacy = ['$window', ($window) ->
controller.$parsers.push (privacy) ->
return unless privacy?
permissions = controller.$modelValue
if isPublic(privacy.name)
permissions.read = ['group:__world__']
newPermissions = permissions.public()
else
permissions.read = [attrs.user]
newPermissions = permissions.private()
permissions.update = [attrs.user]
permissions.delete = [attrs.user]
permissions.admin = [attrs.user]
# Cannot change the $modelValue into a new object
# Just update its properties
for key,val of newPermissions
controller.$modelValue[key] = val
permissions
controller.$modelValue
controller.$render = ->
unless controller.$modelValue.read.length
......
###*
# @ngdoc service
# @name Permissions
#
# @description
# This service can set default permissions to annotations properly and
# offers some utility functions regarding those.
###
class Permissions
this.$inject = ['auth']
constructor: (auth) ->
###*
# @ngdoc method
# @name permissions#private
#
# Sets permissions for a private annotation
# Typical use: annotation.permissions = permissions.private()
###
@private = ->
return {
read: [auth.user]
update: [auth.user]
delete: [auth.user]
admin: [auth.user]
}
###*
# @ngdoc method
# @name permissions#private
#
# Sets permissions for a public annotation
# Typical use: annotation.permissions = permissions.public()
###
@public = ->
return {
read: ['group:__world__']
update: [auth.user]
delete: [auth.user]
admin: [auth.user]
}
###*
# @ngdoc method
# @name permissions#isPublic
#
# @param {Object} annotation annotation to check permissions
#
# This function determines whether the annotation is publicly
# visible(readable) or not.
###
isPublic: (annotation) ->
'group:__world__' in (annotation.permissions?.read or [])
angular.module('h')
.service('permissions', Permissions)
\ 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