Commit 3f864de1958e52ba1be5a7bb22ead8b1c392218a
1 parent
15668d05
Exists in
master
and in
2 other branches
CanCan and Rolify
Showing
11 changed files
with
107 additions
and
3 deletions
Show diff stats
... | ... | @@ -0,0 +1,43 @@ |
1 | +class Ability | |
2 | + include CanCan::Ability | |
3 | + | |
4 | + def initialize(user) | |
5 | + | |
6 | + user ||= User.new | |
7 | + | |
8 | + if user.is_admin? | |
9 | + can :manage, :all | |
10 | + end | |
11 | + | |
12 | + if user.id | |
13 | + can :manage, ActiveAdmin::Page, name: 'Dashboard' | |
14 | + end | |
15 | + | |
16 | + # Define abilities for the passed in user here. For example: | |
17 | + # | |
18 | + # user ||= User.new # guest user (not logged in) | |
19 | + # if user.admin? | |
20 | + # can :manage, :all | |
21 | + # else | |
22 | + # can :read, :all | |
23 | + # end | |
24 | + # | |
25 | + # The first argument to `can` is the action you are giving the user | |
26 | + # permission to do. | |
27 | + # If you pass :manage it will apply to every action. Other common actions | |
28 | + # here are :read, :create, :update and :destroy. | |
29 | + # | |
30 | + # The second argument is the resource the user can perform the action on. | |
31 | + # If you pass :all it will apply to every resource. Otherwise pass a Ruby | |
32 | + # class of the resource. | |
33 | + # | |
34 | + # The third argument is an optional hash of conditions to further filter the | |
35 | + # objects. | |
36 | + # For example, here the user can only update published articles. | |
37 | + # | |
38 | + # can :update, Article, :published => true | |
39 | + # | |
40 | + # See the wiki for details: | |
41 | + # https://github.com/ryanb/cancan/wiki/Defining-Abilities | |
42 | + end | |
43 | +end | ... | ... |
app/models/user.rb
... | ... | @@ -0,0 +1,8 @@ |
1 | +Rolify.configure do |config| | |
2 | + # By default ORM adapter is ActiveRecord. uncomment to use mongoid | |
3 | + # config.use_mongoid | |
4 | + | |
5 | + # Dynamic shortcuts for User class (user.is_admin? like methods). Default is: false | |
6 | + # Enable this feature _after_ running rake db:migrate as it relies on the roles table | |
7 | + config.use_dynamic_shortcuts | |
8 | +end | ... | ... |
config/routes.rb
... | ... | @@ -0,0 +1,19 @@ |
1 | +class RolifyCreateRoles < ActiveRecord::Migration | |
2 | + def change | |
3 | + create_table(:roles) do |t| | |
4 | + t.string :name | |
5 | + t.references :resource, :polymorphic => true | |
6 | + | |
7 | + t.timestamps | |
8 | + end | |
9 | + | |
10 | + create_table(:users_roles, :id => false) do |t| | |
11 | + t.references :user | |
12 | + t.references :role | |
13 | + end | |
14 | + | |
15 | + add_index(:roles, :name) | |
16 | + add_index(:roles, [ :name, :resource_type, :resource_id ]) | |
17 | + add_index(:users_roles, [ :user_id, :role_id ]) | |
18 | + end | |
19 | +end | ... | ... |
db/schema.rb
... | ... | @@ -11,7 +11,18 @@ |
11 | 11 | # |
12 | 12 | # It's strongly recommended that you check this file into your version control system. |
13 | 13 | |
14 | -ActiveRecord::Schema.define(version: 20140513065840) do | |
14 | +ActiveRecord::Schema.define(version: 20140513072121) do | |
15 | + | |
16 | + create_table "roles", force: true do |t| | |
17 | + t.string "name" | |
18 | + t.integer "resource_id" | |
19 | + t.string "resource_type" | |
20 | + t.datetime "created_at" | |
21 | + t.datetime "updated_at" | |
22 | + end | |
23 | + | |
24 | + add_index "roles", ["name", "resource_type", "resource_id"], name: "index_roles_on_name_and_resource_type_and_resource_id" | |
25 | + add_index "roles", ["name"], name: "index_roles_on_name" | |
15 | 26 | |
16 | 27 | create_table "users", force: true do |t| |
17 | 28 | t.string "name" |
... | ... | @@ -32,4 +43,11 @@ ActiveRecord::Schema.define(version: 20140513065840) do |
32 | 43 | add_index "users", ["email"], name: "index_users_on_email", unique: true |
33 | 44 | add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true |
34 | 45 | |
46 | + create_table "users_roles", id: false, force: true do |t| | |
47 | + t.integer "user_id" | |
48 | + t.integer "role_id" | |
49 | + end | |
50 | + | |
51 | + add_index "users_roles", ["user_id", "role_id"], name: "index_users_roles_on_user_id_and_role_id" | |
52 | + | |
35 | 53 | end | ... | ... |
db/seeds.rb