Episodes

Resources

Download Source Code

Summary

# Terminal
rails policy_manager:install:migrations
rails g migration add_role_to_users role:integer
rails g devise:views
rails db:migrate

# Gemfile
gem 'devise'
gem 'gdpr_rails'

# db/migrate/TIMESTAMP_add_role_to_users.rb
class AddRoleToUsers < ActiveRecord::Migration[5.2]
  def change
    add_column :users, :role, :integer, limit: 1, default: 1, after: :remember_created_at
  end
end

# models/user.rb
class User < ApplicationRecord
  include PolicyManager::Concerns::UserBehavior

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable

  enum role: { admin: 0, normal: 1 }
end

# config/initializers/gdpr_rails.rb
PolicyManager::Config.setup do |c|
  c.logout_url = "logout"
  c.from_email = "[email protected]"
  c.admin_email_inbox = "[email protected]"

  # c.user_resource = User # defaults to User
  # c.admin_user_resource = AdminUser # defaults to User

  c.is_admin_method = ->(o){
    # o.is_god? || o.is_admin? || o.is_me? || o.watheva
    o.admin?
  }

  # c.add_rule({name: "cookie", sessionless: true } )
  # c.add_rule({name: "age", validates_on: [:create, :update], blocking: true })
  c.add_rule({name: "privacy_terms", validates_on: [:create, :update], blocking: true })
end

PolicyManager::UserTermsController.send(:include, Devise::Controllers::Helpers)

# class PolicyManager::UserTermsController
# include Devise::Controllers::Helpers
# end

# config/routes.rb
Rails.application.routes.draw do
  mount PolicyManager::Engine => '/policies'
  devise_for :users
  root to: 'welcome#index'
end

# views/devise/registrations/new.html.erb
  <%= content_tag :div, class: :field do %>
    <%= f.check_box :policy_rule_age %>
    <%= f.label :policy_rule_age %>
  <% end if resource.class.method_defined?(:policy_rule_age) %>

  <%= content_tag :div, class: :field do %>
    <%= f.check_box :policy_rule_privacy_terms %>
    <%= f.label :policy_rule_privacy_terms %>
  <% end if resource.class.method_defined?(:policy_rule_privacy_terms) %>

# controllers/application_controller.rb
class ApplicationController < ActionController::Base
  before_action :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up) do |user_params|
      user_params.permit(:email, :password, :password_confirmation, :policy_rule_age, :policy_rule_privacy_terms)
    end
  end
end