Implement @current
method in App.User, and replace throughout codebase where appropriate.
This commit is contained in:
parent
9e0d535483
commit
5b64aeb14d
13 changed files with 53 additions and 61 deletions
|
@ -249,11 +249,7 @@ class App.Controller extends Spine.Controller
|
||||||
false
|
false
|
||||||
|
|
||||||
permissionCheck: (key) ->
|
permissionCheck: (key) ->
|
||||||
userId = App.Session.get('id')
|
App.User.current()?.permission(key)
|
||||||
return false if !userId
|
|
||||||
user = App.User.findNative(userId)
|
|
||||||
return false if !user
|
|
||||||
user.permission(key)
|
|
||||||
|
|
||||||
authenticateCheckRedirect: ->
|
authenticateCheckRedirect: ->
|
||||||
return true if @authenticateCheck()
|
return true if @authenticateCheck()
|
||||||
|
|
|
@ -439,8 +439,6 @@ class App.ControllerNavSidbar extends App.Controller
|
||||||
if @authenticateRequired
|
if @authenticateRequired
|
||||||
@authenticateCheckRedirect()
|
@authenticateCheckRedirect()
|
||||||
|
|
||||||
@user = App.User.find(App.Session.get('id'))
|
|
||||||
|
|
||||||
@render(true)
|
@render(true)
|
||||||
|
|
||||||
@bind('ui:rerender',
|
@bind('ui:rerender',
|
||||||
|
@ -501,7 +499,7 @@ class App.ControllerNavSidbar extends App.Controller
|
||||||
else
|
else
|
||||||
match = false
|
match = false
|
||||||
for permissionName in item.permission
|
for permissionName in item.permission
|
||||||
if !match && @user.permission(permissionName)
|
if !match && @permissionCheck(permissionName)
|
||||||
match = true
|
match = true
|
||||||
groupsUnsorted.push item
|
groupsUnsorted.push item
|
||||||
_.sortBy(groupsUnsorted, (item) -> return item.prio)
|
_.sortBy(groupsUnsorted, (item) -> return item.prio)
|
||||||
|
@ -520,7 +518,7 @@ class App.ControllerNavSidbar extends App.Controller
|
||||||
else
|
else
|
||||||
match = false
|
match = false
|
||||||
for permissionName in item.permission
|
for permissionName in item.permission
|
||||||
if !match && @user && @user.permission(permissionName)
|
if !match && @permissionCheck(permissionName)
|
||||||
match = true
|
match = true
|
||||||
itemsUnsorted.push item
|
itemsUnsorted.push item
|
||||||
|
|
||||||
|
|
|
@ -11,8 +11,7 @@ class SidebarCustomer extends App.Controller
|
||||||
}
|
}
|
||||||
if App.User.exists(@params.customer_id)
|
if App.User.exists(@params.customer_id)
|
||||||
customer = App.User.find(@params.customer_id)
|
customer = App.User.find(@params.customer_id)
|
||||||
currentUser = App.User.find(App.Session.get('id'))
|
if customer.isAccessibleBy(App.User.current(), 'change')
|
||||||
if customer.isAccessibleBy(currentUser, 'change')
|
|
||||||
@item.sidebarActions.push {
|
@item.sidebarActions.push {
|
||||||
title: 'Edit Customer'
|
title: 'Edit Customer'
|
||||||
name: 'customer-edit'
|
name: 'customer-edit'
|
||||||
|
|
|
@ -33,8 +33,6 @@ class Index extends App.ControllerSubContent
|
||||||
render: =>
|
render: =>
|
||||||
return if @initRender && @integration
|
return if @initRender && @integration
|
||||||
|
|
||||||
@user = App.User.find(App.Session.get('id'))
|
|
||||||
|
|
||||||
@initRender = true
|
@initRender = true
|
||||||
integrations = []
|
integrations = []
|
||||||
for key, value of @integrationItems
|
for key, value of @integrationItems
|
||||||
|
@ -44,7 +42,7 @@ class Index extends App.ControllerSubContent
|
||||||
else
|
else
|
||||||
match = false
|
match = false
|
||||||
for permissionName in value.permission
|
for permissionName in value.permission
|
||||||
if !match && @user.permission(permissionName)
|
if !match && @permissionCheck(permissionName)
|
||||||
match = true
|
match = true
|
||||||
value.key = key
|
value.key = key
|
||||||
integrations.push value
|
integrations.push value
|
||||||
|
|
|
@ -312,7 +312,7 @@ class App.Navigation extends App.ControllerWidgetPermanent
|
||||||
@searchContainer.addClass('open')
|
@searchContainer.addClass('open')
|
||||||
@globalSearch.search(query: @query)
|
@globalSearch.search(query: @query)
|
||||||
|
|
||||||
filterNavbar: (values, user, parent = null) ->
|
filterNavbar: (values, parent = null) ->
|
||||||
return _.filter values, (item) =>
|
return _.filter values, (item) =>
|
||||||
if typeof item.callback is 'function'
|
if typeof item.callback is 'function'
|
||||||
data = item.callback() || {}
|
data = item.callback() || {}
|
||||||
|
@ -320,16 +320,16 @@ class App.Navigation extends App.ControllerWidgetPermanent
|
||||||
item[key] = value
|
item[key] = value
|
||||||
|
|
||||||
if !parent? && !item.parent || item.parent is parent
|
if !parent? && !item.parent || item.parent is parent
|
||||||
return @filterNavbarPermissionOk(item, user) &&
|
return @filterNavbarPermissionOk(item) &&
|
||||||
@filterNavbarSettingOk(item)
|
@filterNavbarSettingOk(item)
|
||||||
else
|
else
|
||||||
return false
|
return false
|
||||||
|
|
||||||
filterNavbarPermissionOk: (item, user) ->
|
filterNavbarPermissionOk: (item) ->
|
||||||
return true unless item.permission
|
return true unless item.permission
|
||||||
|
|
||||||
return _.any item.permission, (permissionName) ->
|
return _.any item.permission, (permissionName) =>
|
||||||
return user && user.permission(permissionName)
|
return @permissionCheck(permissionName)
|
||||||
|
|
||||||
filterNavbarSettingOk: (item) ->
|
filterNavbarSettingOk: (item) ->
|
||||||
return true unless item.setting
|
return true unless item.setting
|
||||||
|
@ -343,15 +343,11 @@ class App.Navigation extends App.ControllerWidgetPermanent
|
||||||
level1 = []
|
level1 = []
|
||||||
dropdown = {}
|
dropdown = {}
|
||||||
|
|
||||||
user = undefined
|
level1 = @filterNavbar(navbar)
|
||||||
if App.Session.get('id')
|
|
||||||
user = App.User.find(App.Session.get('id'))
|
|
||||||
|
|
||||||
level1 = @filterNavbar(navbar, user)
|
|
||||||
|
|
||||||
for item in navbar
|
for item in navbar
|
||||||
if item.parent && !dropdown[ item.parent ]
|
if item.parent && !dropdown[ item.parent ]
|
||||||
dropdown[ item.parent ] = @filterNavbar(navbar, user, item.parent)
|
dropdown[ item.parent ] = @filterNavbar(navbar, item.parent)
|
||||||
|
|
||||||
for itemLevel1 in level1
|
for itemLevel1 in level1
|
||||||
if itemLevel1.target is item.parent
|
if itemLevel1.target is item.parent
|
||||||
|
|
|
@ -3,10 +3,7 @@ class Delete
|
||||||
return actions if ui.permissionCheck('ticket.customer')
|
return actions if ui.permissionCheck('ticket.customer')
|
||||||
|
|
||||||
if article.type.name is 'note'
|
if article.type.name is 'note'
|
||||||
user = undefined
|
if App.User.current()?.id == article.created_by_id && ui.permissionCheck('ticket.agent')
|
||||||
if App.Session.get('id') == article.created_by_id
|
|
||||||
user = App.User.find(App.Session.get('id'))
|
|
||||||
if user.permission('ticket.agent')
|
|
||||||
actions.push {
|
actions.push {
|
||||||
name: 'delete'
|
name: 'delete'
|
||||||
type: 'delete'
|
type: 'delete'
|
||||||
|
|
|
@ -15,10 +15,11 @@ class SidebarCustomer extends App.Controller
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
return @item if @ticket && @ticket.customer_id == 1
|
return @item if @ticket && @ticket.customer_id == 1
|
||||||
currentUser = App.User.find(App.Session.get('id'))
|
|
||||||
|
# prevent exceptions if customer model is no available
|
||||||
if @ticket.customer_id && App.User.exists(@ticket.customer_id)
|
if @ticket.customer_id && App.User.exists(@ticket.customer_id)
|
||||||
customer = App.User.find(@ticket.customer_id)
|
customer = App.User.find(@ticket.customer_id)
|
||||||
if customer.isAccessibleBy(currentUser, 'change')
|
if customer?.isAccessibleBy(App.User.current(), 'change')
|
||||||
@item.sidebarActions.push {
|
@item.sidebarActions.push {
|
||||||
title: 'Edit Customer'
|
title: 'Edit Customer'
|
||||||
name: 'customer-edit'
|
name: 'customer-edit'
|
||||||
|
|
|
@ -107,8 +107,6 @@ class ActionRow extends App.ObserverActionRow
|
||||||
@navigate("ticket/create/customer/#{user.id}")
|
@navigate("ticket/create/customer/#{user.id}")
|
||||||
|
|
||||||
actions: (user) =>
|
actions: (user) =>
|
||||||
currentUser = App.User.find(App.Session.get('id'))
|
|
||||||
|
|
||||||
actions = [
|
actions = [
|
||||||
{
|
{
|
||||||
name: 'history'
|
name: 'history'
|
||||||
|
@ -122,7 +120,7 @@ class ActionRow extends App.ObserverActionRow
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
if user.isAccessibleBy(currentUser, 'change')
|
if user.isAccessibleBy(App.User.current(), 'change')
|
||||||
actions.unshift {
|
actions.unshift {
|
||||||
name: 'edit'
|
name: 'edit'
|
||||||
title: 'Edit'
|
title: 'Edit'
|
||||||
|
|
|
@ -12,9 +12,8 @@ class Widget extends App.Controller
|
||||||
@verifyLater(user.id)
|
@verifyLater(user.id)
|
||||||
'user_signup_verify'
|
'user_signup_verify'
|
||||||
)
|
)
|
||||||
currentUserId = App.Session.get('id')
|
user = App.User.current()
|
||||||
return if !currentUserId
|
@verifyLater(user.id) if user?
|
||||||
@verifyLater(currentUserId)
|
|
||||||
|
|
||||||
verifyLater: (userId) =>
|
verifyLater: (userId) =>
|
||||||
delay = =>
|
delay = =>
|
||||||
|
|
|
@ -651,15 +651,11 @@ class _taskManagerSingleton extends App.Controller
|
||||||
App.Event.trigger 'taskbar:init'
|
App.Event.trigger 'taskbar:init'
|
||||||
|
|
||||||
# initial load of permanent tasks
|
# initial load of permanent tasks
|
||||||
user_id = App.Session.get('id')
|
|
||||||
user = undefined
|
|
||||||
if user_id
|
|
||||||
user = App.User.find(user_id)
|
|
||||||
permanentTask = App.Config.get('permanentTask')
|
permanentTask = App.Config.get('permanentTask')
|
||||||
taskCount = 0
|
taskCount = 0
|
||||||
if permanentTask
|
if permanentTask
|
||||||
for key, config of permanentTask
|
for key, config of permanentTask
|
||||||
if !config.permission || (user && user.permission(config.permission))
|
if !config.permission || @permissionCheck(config.permission)
|
||||||
taskCount += 1
|
taskCount += 1
|
||||||
do (key, config, taskCount) =>
|
do (key, config, taskCount) =>
|
||||||
App.Delay.set(
|
App.Delay.set(
|
||||||
|
|
|
@ -247,10 +247,10 @@ class App.Ticket extends App.Model
|
||||||
result
|
result
|
||||||
|
|
||||||
editable: (permission = 'change') ->
|
editable: (permission = 'change') ->
|
||||||
user_id = App.Session.get('id')
|
user = App.User.current()
|
||||||
return true if user_id is @customer_id
|
return false if !user?
|
||||||
return false if !App.User.exists(user_id)
|
return true if user.id is @customer_id
|
||||||
group_ids = App.User.find(user_id).allGroupIds(permission)
|
group_ids = user.allGroupIds(permission)
|
||||||
for local_group_id in group_ids
|
for local_group_id in group_ids
|
||||||
if local_group_id.toString() is @group_id.toString()
|
if local_group_id.toString() is @group_id.toString()
|
||||||
return true
|
return true
|
||||||
|
|
|
@ -346,3 +346,7 @@ class App.User extends App.Model
|
||||||
return false if @organization_id is null
|
return false if @organization_id is null
|
||||||
return false if requester.organization_id is null
|
return false if requester.organization_id is null
|
||||||
@organization_id == requester.organization_id
|
@organization_id == requester.organization_id
|
||||||
|
|
||||||
|
# Do NOT modify the return value of this method!
|
||||||
|
# It is a direct reference to a value in the App.User.irecords object.
|
||||||
|
@current: App.Session.get
|
||||||
|
|
|
@ -2,7 +2,8 @@ window.onload = function() {
|
||||||
|
|
||||||
test('test current user behaviour by updating session user via assets', function() {
|
test('test current user behaviour by updating session user via assets', function() {
|
||||||
|
|
||||||
// load user
|
// Wenn App.User updated through asset and set as session user
|
||||||
|
// expect App.Session.get with new values
|
||||||
App.User.refresh([{
|
App.User.refresh([{
|
||||||
"login": "hh@example.com",
|
"login": "hh@example.com",
|
||||||
"firstname": "Harald",
|
"firstname": "Harald",
|
||||||
|
@ -18,11 +19,7 @@ test('test current user behaviour by updating session user via assets', function
|
||||||
"asdf": "",
|
"asdf": "",
|
||||||
"id": 6
|
"id": 6
|
||||||
}]);
|
}]);
|
||||||
|
|
||||||
// set session user
|
|
||||||
App.Session.set(6)
|
App.Session.set(6)
|
||||||
|
|
||||||
// verify attributes
|
|
||||||
equal(App.Session.get('id'), 6)
|
equal(App.Session.get('id'), 6)
|
||||||
equal(App.Session.get('login'), 'hh@example.com')
|
equal(App.Session.get('login'), 'hh@example.com')
|
||||||
equal(App.Session.get('vip'), false)
|
equal(App.Session.get('vip'), false)
|
||||||
|
@ -32,7 +29,8 @@ test('test current user behaviour by updating session user via assets', function
|
||||||
equal(App.Session.get().custom_key, undefined)
|
equal(App.Session.get().custom_key, undefined)
|
||||||
equal(App.Session.get().not_existing, undefined)
|
equal(App.Session.get().not_existing, undefined)
|
||||||
|
|
||||||
// update session user via assets
|
// Wenn App.User updated through asset
|
||||||
|
// expect App.Session.get with new values
|
||||||
App.User.refresh([{
|
App.User.refresh([{
|
||||||
"login": "hh_new@example.com",
|
"login": "hh_new@example.com",
|
||||||
"firstname": "Harald",
|
"firstname": "Harald",
|
||||||
|
@ -48,8 +46,6 @@ test('test current user behaviour by updating session user via assets', function
|
||||||
"asdf": "",
|
"asdf": "",
|
||||||
"id": 6
|
"id": 6
|
||||||
}]);
|
}]);
|
||||||
|
|
||||||
// verify attributes
|
|
||||||
equal(App.Session.get('id'), 6)
|
equal(App.Session.get('id'), 6)
|
||||||
equal(App.Session.get('login'), 'hh_new@example.com')
|
equal(App.Session.get('login'), 'hh_new@example.com')
|
||||||
equal(App.Session.get('vip'), false)
|
equal(App.Session.get('vip'), false)
|
||||||
|
@ -59,7 +55,8 @@ test('test current user behaviour by updating session user via assets', function
|
||||||
equal(App.Session.get().custom_key, undefined)
|
equal(App.Session.get().custom_key, undefined)
|
||||||
equal(App.Session.get().not_existing, undefined)
|
equal(App.Session.get().not_existing, undefined)
|
||||||
|
|
||||||
// clear session
|
// Wenn App.Session is reseted to inital
|
||||||
|
// expect undefined for all
|
||||||
App.Session.init()
|
App.Session.init()
|
||||||
equal(App.Session.get(), undefined)
|
equal(App.Session.get(), undefined)
|
||||||
equal(App.Session.get('id'), undefined)
|
equal(App.Session.get('id'), undefined)
|
||||||
|
@ -67,6 +64,19 @@ test('test current user behaviour by updating session user via assets', function
|
||||||
equal(App.Session.get('vip'), undefined)
|
equal(App.Session.get('vip'), undefined)
|
||||||
equal(App.Session.get('custom_key'), undefined)
|
equal(App.Session.get('custom_key'), undefined)
|
||||||
|
|
||||||
|
// When App.Session is set and set to undefined or null,
|
||||||
|
// expect @current() to return null
|
||||||
|
App.Session.set(6)
|
||||||
|
App.Session.set(undefined)
|
||||||
|
equal(App.User.current(), null, 'with no active session')
|
||||||
|
App.Session.set(null)
|
||||||
|
equal(App.User.current(), null, 'with no active session')
|
||||||
|
|
||||||
|
// When App.Session is set with an invalid (not existing) user ID,
|
||||||
|
// expect @current() to return null
|
||||||
|
App.Session.set(100)
|
||||||
|
equal(App.User.current(), null, 'with invalid session user ID')
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in a new issue