Skip to content

Commit 2fb5362

Browse files
authored
Merge pull request #11 from tb/kg_roles
Kg roles
2 parents 3574a93 + 335cc7b commit 2fb5362

25 files changed

+320
-38
lines changed

Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ gem 'jsonapi-resources'
2828
gem 'factory_girl'
2929
gem 'faker'
3030
gem 'devise_token_auth'
31+
gem 'cancan'
32+
gem 'rolify'
33+
gem 'pry'
3134

3235
group :development, :test do
3336
# Call 'byebug' anywhere in the code to stop execution and get a debugger console

Gemfile.lock

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ GEM
4242
bcrypt (3.1.11)
4343
builder (3.2.3)
4444
byebug (9.0.6)
45+
cancan (1.6.10)
46+
coderay (1.1.1)
4547
concurrent-ruby (1.0.5)
4648
devise (4.2.0)
4749
bcrypt (~> 3.0)
@@ -86,6 +88,10 @@ GEM
8688
mini_portile2 (~> 2.1.0)
8789
orm_adapter (0.5.0)
8890
pg (0.20.0)
91+
pry (0.10.4)
92+
coderay (~> 1.1.0)
93+
method_source (~> 0.8.1)
94+
slop (~> 3.4)
8995
puma (3.8.2)
9096
rack (2.0.1)
9197
rack-cors (0.4.1)
@@ -120,6 +126,7 @@ GEM
120126
ffi (>= 0.5.0)
121127
responders (2.3.0)
122128
railties (>= 4.2.0, < 5.1)
129+
rolify (5.1.0)
123130
rspec-core (3.5.4)
124131
rspec-support (~> 3.5.0)
125132
rspec-expectations (3.5.0)
@@ -137,6 +144,7 @@ GEM
137144
rspec-mocks (~> 3.5.0)
138145
rspec-support (~> 3.5.0)
139146
rspec-support (3.5.0)
147+
slop (3.6.0)
140148
spring (2.0.1)
141149
activesupport (>= 4.2)
142150
spring-watcher-listen (2.0.1)
@@ -164,16 +172,19 @@ PLATFORMS
164172

165173
DEPENDENCIES
166174
byebug
175+
cancan
167176
devise_token_auth
168177
factory_girl
169178
faker
170179
foreman
171180
jsonapi-resources
172181
listen (~> 3.0.5)
173182
pg (~> 0.18)
183+
pry
174184
puma (~> 3.0)
175185
rack-cors
176186
rails (~> 5.0.2)
187+
rolify
177188
rspec-rails
178189
spring
179190
spring-watcher-listen (~> 2.0.0)

app/controllers/application_controller.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@ class ApplicationController < ActionController::Base
22
# Prevent CSRF attacks by raising an exception.
33
# For APIs, you may want to use :null_session instead.
44
# protect_from_forgery with: :null_session
5+
#
6+
rescue_from CanCan::AccessDenied do |exception|
7+
render json: { message: "You don't have permissions." }, status: :forbidden
8+
end
59
end

app/controllers/roles_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class RolesController < AuthorizedController
2+
end

app/controllers/users_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
class UsersController < AuthorizedController
2+
load_and_authorize_resource
23
end

app/models/ability.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Ability
2+
include CanCan::Ability
3+
4+
def initialize(user)
5+
user ||= User.new # guest user (not logged in)
6+
if user.is_admin?
7+
can :manage, :all
8+
else
9+
can :manage, Post
10+
can :manage, Category
11+
can :manage, Comment
12+
can :update, User, id: user.id
13+
end
14+
end
15+
end

app/models/role.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Role < ApplicationRecord
2+
has_and_belongs_to_many :users, :join_table => :users_roles
3+
4+
belongs_to :resource,
5+
:polymorphic => true,
6+
:optional => true
7+
8+
validates :resource_type,
9+
:inclusion => { :in => Rolify.resource_types },
10+
:allow_nil => true
11+
12+
scopify
13+
end

app/models/user.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
class User < ActiveRecord::Base
2+
rolify
3+
has_and_belongs_to_many :roles, :join_table => :users_roles
4+
25
# Include default devise modules.
36
devise :database_authenticatable, :registerable,
47
:recoverable, :rememberable, :trackable, :validatable,
58
:confirmable
69
include DeviseTokenAuth::Concerns::User
710

811
scope :email_contains, -> (value) { where('email ILIKE ?', "%#{value.join}%") }
12+
13+
def token_validation_response
14+
self.as_json(except: [
15+
:tokens, :created_at, :updated_at
16+
]).merge(roles: self.roles.map(&:name))
17+
end
918
end

app/resources/role_resource.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class RoleResource < JSONAPI::Resource
2+
attributes :name
3+
end

app/resources/user_resource.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
class UserResource < JSONAPI::Resource
22
extend ModelFilter
3-
attributes :email, :confirmed_at, :created_at
3+
attributes :email, :confirmed_at, :created_at, :roles
44

55
paginator :paged
66
model_filters :email_contains
7+
8+
def roles
9+
@model.roles.pluck(:name)
10+
end
11+
12+
def roles=(roles)
13+
@model.roles.destroy_all
14+
roles.map do |role|
15+
@model.add_role role
16+
end
17+
end
718
end

0 commit comments

Comments
 (0)