import Ember from 'ember'
export default Ember.Controller.extend
  adapters: []
  panda_authorizations: [] # set by route

  active_authorizations: []

  actions:
    set_permission: (authorization, permission) ->
      new_roles = []
      @get('active_authorizations').forEach (role) ->
        if role.match(new RegExp("-#{authorization.type}-#{authorization.target}$"))
          new_roles.push "#{permission}-#{authorization.type}-#{authorization.target}"
        else
          # different authorization
          new_roles.push role
      @set 'active_authorizations', new_roles
    save_permissions: ->
      user_roles = []
      @get('active_authorizations').forEach (role) ->
        user_roles.push role unless role.match(/^ban-/)
      user = @get('model')
      user.set 'role_names', user_roles
      user.save().then => @set 'globals.flash_message', 'Permissions saved'
