Init version of permission management of personal tokens.

This commit is contained in:
Martin Edenhofer 2016-08-16 09:09:09 +02:00
parent 3bf32d51e4
commit e7960ab03b
5 changed files with 68 additions and 10 deletions

View file

@ -1,7 +1,7 @@
class Index extends App.ControllerContent class Index extends App.ControllerContent
requiredPermission: 'user_preferences.access_token' requiredPermission: 'user_preferences.access_token'
events: events:
'click [data-type=delete]': 'delete' 'click .js-delete': 'delete'
'submit form.js-create': 'create' 'submit form.js-create': 'create'
constructor: -> constructor: ->
@ -22,24 +22,35 @@ class Index extends App.ControllerContent
type: 'GET' type: 'GET'
url: "#{@apiPath}/user_access_token" url: "#{@apiPath}/user_access_token"
success: (data) => success: (data) =>
tokens = data.tokens
# verify is rerender is needed # verify is rerender is needed
if !force && @lastestUpdated && data && data[0] && @lastestUpdated.updated_at is data[0].updated_at if !force && @lastestUpdated && tokens && tokens[0] && @lastestUpdated.updated_at is tokens[0].updated_at
return return
@lastestUpdated = data[0] @lastestUpdated = tokens[0]
@data = data @tokens = data.tokens
@permissions = data.permissions
@render() @render()
) )
render: => render: =>
@html App.view('profile/token_access')( @html App.view('profile/token_access')(
tokens: @data tokens: @tokens
permissions: @permissions
) )
create: (e) => create: (e) =>
e.preventDefault() e.preventDefault()
params = @formParam(e.target) params = @formParam(e.target)
# check if min one permission exists
if _.isEmpty(params['permission'])
alert('Min. one permission is needed!')
return
if !_.isArray(params['permission'])
params['permission'] = [params['permission']]
@ajax( @ajax(
id: 'user_access_token_create' id: 'user_access_token_create'
type: 'POST' type: 'POST'

View file

@ -1,4 +1,4 @@
<div class="checkbox <%= @attribute.class %> checkbox"> <div class="checkbox <%= @attribute.class %>">
<% for permission in @permissions: %> <% for permission in @permissions: %>
<% if !permission.name.match(/\./): %> <% if !permission.name.match(/\./): %>
<label class="inline-label checkbox-replacement"> <label class="inline-label checkbox-replacement">

View file

@ -14,6 +14,33 @@
</div> </div>
<div class="controls"><input id="token-label" type="text" name="label" value="" class="form-control js-input" required></div> <div class="controls"><input id="token-label" type="text" name="label" value="" class="form-control js-input" required></div>
</div> </div>
<div class="permission form-group checkbox">
<div class="checkbox">
<% for permission in @permissions: %>
<% if !permission.name.match(/\./): %>
<label class="inline-label checkbox-replacement">
<input type="checkbox" value="<%= permission.name %>" name="permission" <% if @params && _.contains(@params.permissions, permission.id): %>checked<% end %> <% if permission.preferences.disabled: %>disabled<% end %>/>
<%- @Icon('checkbox', 'icon-unchecked') %>
<%- @Icon('checkbox-checked', 'icon-checked') %>
<span class="label-text"><%= permission.name %> - <span class="help-text"><%- @T(permission.note) %></span></span>
</label>
<% else: %>
<div style="padding-left: 20px;" class="js-subPermissionList">
<label class="inline-label checkbox-replacement">
<input type="checkbox" value="<%= permission.name %>" name="permission" <% if @params && _.contains(@params.permissions, permission.id): %>checked<% end %> <% if permission.preferences.disabled: %>disabled<% end %>/>
<%- @Icon('checkbox', 'icon-unchecked') %>
<%- @Icon('checkbox-checked', 'icon-checked') %>
<span class="label-text"><%= permission.name.replace(/^.+?\./, '') %> - <span class="help-text"><%- @T(permission.note) %></span></span>
</label>
</div>
<% end %>
<% end %>
</div>
</div>
<button class="btn btn--primary js-submit"><%- @T('Create') %></button> <button class="btn btn--primary js-submit"><%- @T('Create') %></button>
</form> </form>
@ -24,6 +51,7 @@
<thead> <thead>
<tr> <tr>
<th><%- @T('Name') %></th> <th><%- @T('Name') %></th>
<th><%- @T('Permission') %></th>
<th><%- @T('Created') %></th> <th><%- @T('Created') %></th>
<!-- <!--
<th><%- @T('Expires') %></th> <th><%- @T('Expires') %></th>
@ -40,6 +68,7 @@
<% for token in @tokens: %> <% for token in @tokens: %>
<tr> <tr>
<td><%= token.label %></td> <td><%= token.label %></td>
<td><% if token.preferences && token.preferences.permission: %><%= token.preferences.permission.join(', ') %><% end %></td>
<td><%- @humanTime(token.created_at) %></td> <td><%- @humanTime(token.created_at) %></td>
<!-- <!--
<td><%- @humanTime(token.expired_at) %></td> <td><%- @humanTime(token.expired_at) %></td>
@ -47,7 +76,7 @@
--> -->
<td class="settings-list-controls"> <td class="settings-list-controls">
<div> <div>
<a class="settings-list-control" href="#" data-token-id="<%- token.id %>" data-type="delete" title="<%- @Ti('Delete') %>"><%- @Icon('trash') %></a> <a class="settings-list-control js-delete" href="#" data-token-id="<%- token.id %>" title="<%- @Ti('Delete') %>"><%- @Icon('trash') %></a>
</div> </div>
</tr> </tr>
<% end %> <% end %>

View file

@ -1,7 +1,7 @@
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/ # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
class UserAccessTokenController < ApplicationController class UserAccessTokenController < ApplicationController
before_action :authentication_check before_action { authentication_check(permission: 'user_preferences.access_token') }
def index def index
tokens = Token.where(action: 'api', persistent: true, user_id: current_user.id).order('updated_at DESC, label ASC') tokens = Token.where(action: 'api', persistent: true, user_id: current_user.id).order('updated_at DESC, label ASC')
@ -12,7 +12,25 @@ class UserAccessTokenController < ApplicationController
attributes.delete('name') attributes.delete('name')
token_list.push attributes token_list.push attributes
} }
model_index_render_result(token_list) local_permissions = current_user.permissions
local_permissions_new = {}
local_permissions.each { |key, _value|
keys = Object.const_get('Permission').with_parents(key)
keys.each { |local_key|
next if local_permissions_new[local_key]
local_permissions_new[local_key] = false
}
}
permissions = []
Permission.all.order(:name).each { |permission|
next if !local_permissions_new.key?(permission.name)
permissions.push permission
}
render json: {
tokens: token_list,
permissions: permissions,
}, status: :ok
end end
def create def create

View file

@ -1,7 +1,7 @@
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/ # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
class UserDevicesController < ApplicationController class UserDevicesController < ApplicationController
before_action :authentication_check before_action { authentication_check(permission: 'user_preferences.device') }
def index def index
devices = UserDevice.where(user_id: current_user.id).order('updated_at DESC, name ASC') devices = UserDevice.where(user_id: current_user.id).order('updated_at DESC, name ASC')