Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pundit for user roles #76

Merged
merged 5 commits into from
Oct 20, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ gem 'jquery-rails'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'

# User authentication
# User authentication and roles
gem 'devise', '~> 4.3'
gem 'pundit'

# Geocoding addresses to lat/long
gem 'geocoder', '~> 1.4'
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ GEM
pry (>= 0.10.4)
public_suffix (2.0.5)
puma (3.8.2)
pundit (1.1.0)
activesupport (>= 3.0.0)
rack (2.0.2)
rack-test (0.6.3)
rack (>= 1.0)
Expand Down Expand Up @@ -298,6 +300,7 @@ DEPENDENCIES
pry-byebug
pry-rails
puma (~> 3.7)
pundit
rails (~> 5.1.1)
refills
rspec-rails
Expand Down
1 change: 1 addition & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class ApplicationController < ActionController::Base
include Pundit
protect_from_forgery with: :exception
before_action :configure_permitted_parameters, if: :devise_controller?

Expand Down
6 changes: 6 additions & 0 deletions app/models/assignment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Assignment < ApplicationRecord
belongs_to :user
belongs_to :role

validates_presence_of :user_id, :role_id
end
6 changes: 6 additions & 0 deletions app/models/role.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Role < ApplicationRecord
has_many :assignments
has_many :users, through: :assignments

validates :name, presence: true, uniqueness: true
end
7 changes: 7 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,20 @@ class User < ApplicationRecord
has_many :organization_users
has_many :organizations, through: :organization_users

has_many :assignments
has_many :roles, through: :assignments

geocoded_by :full_street_address
after_validation :geocode, if: ->(user){ user.full_street_address.present? and user.full_street_address_changed? }

def name
"#{first_name} #{last_name}"
end

def role?(role)
roles.any? { |r| r.name.underscore.to_sym == role }
end

# TODO: Add support for multiple interests
scope :search_by_interest, (lambda { |interest|
joins(:interests).merge(Interest.where("interest ILIKE ?", interest)) if interest.present? })
Expand Down
53 changes: 53 additions & 0 deletions app/policies/application_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
class ApplicationPolicy
attr_reader :user, :record

def initialize(user, record)
@user = user
@record = record
end

def index?
false
end

def show?
scope.where(:id => record.id).exists?
end

def create?
false
end

def new?
create?
end

def update?
false
end

def edit?
update?
end

def destroy?
false
end

def scope
Pundit.policy_scope!(user, record.class)
end

class Scope
attr_reader :user, :scope

def initialize(user, scope)
@user = user
@scope = scope
end

def resolve
scope
end
end
end
9 changes: 9 additions & 0 deletions db/migrate/20171016210855_create_roles.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateRoles < ActiveRecord::Migration[5.1]
def change
create_table :roles do |t|
t.string :name

t.timestamps
end
end
end
10 changes: 10 additions & 0 deletions db/migrate/20171016211209_create_assignments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateAssignments < ActiveRecord::Migration[5.1]
def change
create_table :assignments do |t|
t.references :user, foreign_key: true
t.references :role, foreign_key: true

t.timestamps
end
end
end
27 changes: 26 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,20 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20171008224623) do
ActiveRecord::Schema.define(version: 20171016211209) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "assignments", force: :cascade do |t|
t.bigint "user_id"
t.bigint "role_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["role_id"], name: "index_assignments_on_role_id"
t.index ["user_id"], name: "index_assignments_on_user_id"
end

create_table "interests", force: :cascade do |t|
t.string "interest"
t.datetime "created_at", null: false
Expand Down Expand Up @@ -75,6 +84,12 @@
t.datetime "updated_at", null: false
end

create_table "roles", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "skills", force: :cascade do |t|
t.string "skill", null: false
t.datetime "created_at", null: false
Expand Down Expand Up @@ -117,4 +132,14 @@
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end

create_table "users_roles", id: false, force: :cascade do |t|
t.bigint "user_id"
t.bigint "role_id"
t.index ["role_id"], name: "index_users_roles_on_role_id"
t.index ["user_id", "role_id"], name: "index_users_roles_on_user_id_and_role_id"
t.index ["user_id"], name: "index_users_roles_on_user_id"
end

add_foreign_key "assignments", "roles"
add_foreign_key "assignments", "users"
end
6 changes: 6 additions & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
password: 'password',
))

puts "Populating roles..."
Role.destroy_all
ROLES = ['admin',
'organization owner'
].map { |role| Role.create(name: role) }

puts "Populating skills..."
Skill.destroy_all
SKILLS = [
Expand Down
6 changes: 6 additions & 0 deletions spec/factories/assignments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FactoryGirl.define do
factory :assignment do
user nil
role nil
end
end
5 changes: 5 additions & 0 deletions spec/factories/roles.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FactoryGirl.define do
factory :role do
name "Role"
end
end
19 changes: 19 additions & 0 deletions spec/models/assignment_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'rails_helper'

RSpec.describe Assignment, type: :model do
let(:user) { FactoryGirl.create :user }
let(:role) { FactoryGirl.create :role }
let(:assignment) { FactoryGirl.build_stubbed(:assignment, user_id: user.id, role_id: role.id) }

describe "validations" do
it "is valid with valid attributes" do
expect(assignment).to be_valid
end
it "is not valid without a user" do
expect(FactoryGirl.build_stubbed(:assignment, user_id: nil, role_id: role.id)).to_not be_valid
end
it "is not valid without a role" do
expect(FactoryGirl.build_stubbed(:assignment, user_id: user.id, role_id: nil)).to_not be_valid
end
end
end
14 changes: 14 additions & 0 deletions spec/models/role_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'rails_helper'

RSpec.describe Role, type: :model do
let(:role) { FactoryGirl.build_stubbed :role }

describe "validations" do
it "is valid with valid attributes" do
expect(role).to be_valid
end
it "is not valid without a name" do
expect(FactoryGirl.build_stubbed(:role, name: nil)).to_not be_valid
end
end
end
5 changes: 5 additions & 0 deletions spec/policies/application_policy_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require "rails_helper"

describe ApplicationPolicy do
it "is not implemented"
end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "pundit/rspec"

# This file was generated by the `rails generate rspec:install` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# The generated `.rspec` file contains `--require spec_helper` which will cause
Expand Down