From 0418924d8eccfbe0a55e02309da74e97b8a11998 Mon Sep 17 00:00:00 2001 From: Stella Rouzi Date: Fri, 18 Jul 2014 14:43:49 +0300 Subject: [PATCH] add is_admin attribute to users --- app/controllers/admin/users_controller.rb | 1 + app/models/admin_ability.rb | 12 ++++-------- app/models/user.rb | 2 +- app/views/admin/users/_roles.html.haml | 12 ++++++++++++ app/views/admin/users/index.html.haml | 13 +------------ db/migrate/20140718103856_add_is_admin_to_users.rb | 5 +++++ db/schema.rb | 1 + 7 files changed, 25 insertions(+), 21 deletions(-) create mode 100644 app/views/admin/users/_roles.html.haml create mode 100644 db/migrate/20140718103856_add_is_admin_to_users.rb diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index 178f3ae5..49ea1676 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -4,6 +4,7 @@ module Admin def index @users = User.all + @roles = Role.all.where(resource_type: nil) end def show diff --git a/app/models/admin_ability.rb b/app/models/admin_ability.rb index e4deb085..040af215 100644 --- a/app/models/admin_ability.rb +++ b/app/models/admin_ability.rb @@ -48,8 +48,6 @@ class AdminAbility cannot :manage, :schedule can :manage, :schedule -# cannot :manage, Registration -# can :manage, Registration, conference: { id: conf_ids_for_organizer} # Authorize explicitely, so that it doesn't look for a 'conference_id' can :manage, :volunteer @@ -64,9 +62,11 @@ class AdminAbility # can :manage, Role, resource_id: conf_ids_for_organizer end - if user.is_organizer? # A user can have 'organizer' role not associated to specific conference + if user.is_admin # is_admin is an attribute of User can :create, Conference - # Can manage /admin/conference # any organizer of any conference and anyone who can create a conf can view the /admin/conference + can :index, Conference # this will allow the Conference to appear in the menu + can :view, Conference # for /admin/conference overview + can :manage, User # to make other users admins end ## Authorization for CfP @@ -107,9 +107,5 @@ class AdminAbility can :manage, Vday, conference_id: conf_ids_for_volunteer_coordinator can :manage, :volunteer end - - # Allow access to event_attachments that belong to events (via event_id), which - # events belong to a conference (via conference_id) that has an organizer role for current user -# can :manage, EventAttachment, event_id: (Event.where(conference_id: conf_ids_for_organizer).pluck(:id)) end end diff --git a/app/models/user.rb b/app/models/user.rb index b4db9332..6f51dc4a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -53,7 +53,7 @@ class User < ActiveRecord::Base end def setup_role - roles << Role.where(name: 'organizer') if User.count == 0 + is_admin = true if User.count == 0 roles << Role.where(name: 'participant') if roles.empty? end diff --git a/app/views/admin/users/_roles.html.haml b/app/views/admin/users/_roles.html.haml new file mode 100644 index 00000000..6b225689 --- /dev/null +++ b/app/views/admin/users/_roles.html.haml @@ -0,0 +1,12 @@ +- if current_user == user + You cannot modify your own role! + %br + %button{:class=> "btn btn-danger", "data-dismiss"=> "modal", "aria-hidden"=>"true"} + Cancel +- else + = "Give #{user.name} (#{user.email}) the following roles:" + = semantic_form_for(user, :url => admin_user_path(user), :method => :put) do |f| + = f.input :roles, :label => false, collection: @roles + %button{:class=> "btn btn-danger", "data-dismiss"=> "modal", "aria-hidden"=>"true"} + Cancel + = f.action :submit, :as => :button, :button_html => {:value => "Save", :class => "btn btn-primary"} diff --git a/app/views/admin/users/index.html.haml b/app/views/admin/users/index.html.haml index 5b7880d7..89ac5736 100644 --- a/app/views/admin/users/index.html.haml +++ b/app/views/admin/users/index.html.haml @@ -49,18 +49,7 @@ %h3{:id => "role-selector-header-#{user.id}"} Modifying Roles .modal-body - - if current_user == user - You cannot modify your own role! - %br - %button{:class=> "btn btn-danger", "data-dismiss"=> "modal", "aria-hidden"=>"true"} - Cancel - - else - = "Give #{user.name} (#{user.email}) the following roles:" - = semantic_form_for(user, :url => admin_user_path(user), :method => :put) do |f| - = f.input :roles, :label => false - %button{:class=> "btn btn-danger", "data-dismiss"=> "modal", "aria-hidden"=>"true"} - Cancel - = f.action :submit, :as => :button, :button_html => {:value => "Save", :class => "btn btn-primary"} + = render partial: 'roles', locals: { user: user } - if can? :update, Role =link_to "#{user.roles.map { |role| role.name }.join ', '}", "#", "data-toggle" => "modal", "data-target" => "#user-role-selection-#{user.id}",id: "user-modify-role-#{user.id}" - else diff --git a/db/migrate/20140718103856_add_is_admin_to_users.rb b/db/migrate/20140718103856_add_is_admin_to_users.rb new file mode 100644 index 00000000..def2bbb0 --- /dev/null +++ b/db/migrate/20140718103856_add_is_admin_to_users.rb @@ -0,0 +1,5 @@ +class AddIsAdminToUsers < ActiveRecord::Migration + def change + add_column :users, :is_admin, :boolean + end +end diff --git a/db/schema.rb b/db/schema.rb index 308c343a..086eb71a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -474,6 +474,7 @@ ActiveRecord::Schema.define(version: 20140724113107) do t.string "tshirt" t.string "languages" t.text "volunteer_experience" + t.boolean "is_admin" end add_index "users", ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true