rails_apps_pages 0.5.5 → 0.5.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1dbc7ddf891986e0fcc5c62289dd8ab0cf611246
4
- data.tar.gz: bfe79b9412e25c244128171403ca3466a44ce65c
3
+ metadata.gz: 2666de6172f8535a6a2f744f46aaec5efa3300e8
4
+ data.tar.gz: ff175f2895625425321ae15cf3147c9071eeb650
5
5
  SHA512:
6
- metadata.gz: 472a90a46c599ca28706774f77dd323800ca56477ed3d257ab743ca76272b90d0083efd04c21bcf291e35c3246716615254c1922f00f14b7648789f534956a87
7
- data.tar.gz: d10b12556fba71620f699e84a9ecaf3da0f71ef0070829b9d2c7b54602d3d8108a72f8737f2c277df16e6a838adabaae2900f0c763628a04abbeb131467cfe6c
6
+ metadata.gz: d00e4ea4ef71c36ea4599f7855384ce1d833964fd93a804a76f8d4eba02e200853d6b61ceeb7111574404ba0b9c06b4bfb3b8565c1a7b523989a942ba93898bb
7
+ data.tar.gz: c24152b386a11a645d580e9c1875efbeeb1d0513f1f6fb2b75781647700ecefb8343e8d9072fbd4944f2da20d6f82dd98ff2859623ac40e6b386bf0324767078
data/CHANGELOG.textile CHANGED
@@ -1,5 +1,9 @@
1
1
  h1. CHANGELOG
2
2
 
3
+ h3. 0.5.6 June 19, 2014
4
+
5
+ * add files to integrate Pundit with OmniAuth
6
+
3
7
  h3. 0.5.5 June 19, 2014
4
8
 
5
9
  * don't generate 'users' pages when generating 'authorized' pages
@@ -5,14 +5,23 @@ module Pages
5
5
  class AuthorizedGenerator < ::Rails::Generators::Base
6
6
  source_root File.expand_path("../templates", __FILE__)
7
7
 
8
- desc "Create pages to accompany a User model when authorization is available."
8
+ desc "Add Pundit for authorization. A User model with roles must be available."
9
9
 
10
- def create_page
11
- ### assumes we are using Devise for authentication
12
- ### assumes we are using Pundit for authorization
10
+ def add_pundit
11
+ copy_file 'pundit.rb', 'config/initializers/pundit.rb'
13
12
  copy_file 'users/_user.html.erb', 'app/views/users/_user.html.erb'
14
- copy_file 'users_controller.rb', 'app/controllers/users_controller.rb'
15
- copy_file 'user_policy.rb', 'app/policies/user_policy.rb'
13
+ end
14
+
15
+ def add_if_devise
16
+ return unless File.exists?('config/initializers/devise.rb')
17
+ copy_file 'devise/users_controller.rb', 'app/controllers/users_controller.rb'
18
+ copy_file 'devise/user_policy.rb', 'app/policies/user_policy.rb'
19
+ end
20
+
21
+ def add_if_omniauth
22
+ return unless File.exists?('config/initializers/omniauth.rb')
23
+ copy_file 'omniauth/users_controller.rb', 'app/controllers/users_controller.rb'
24
+ copy_file 'omniauth/user_policy.rb', 'app/policies/user_policy.rb'
16
25
  end
17
26
 
18
27
  end
@@ -0,0 +1,17 @@
1
+ class UserPolicy
2
+ attr_reader :current_user, :model
3
+
4
+ def initialize(current_user, model)
5
+ @current_user = current_user
6
+ @user = model
7
+ end
8
+
9
+ def index?
10
+ @current_user.admin?
11
+ end
12
+
13
+ def show?
14
+ @current_user.admin? or @current_user == @user
15
+ end
16
+
17
+ end
@@ -0,0 +1,34 @@
1
+ class UsersController < ApplicationController
2
+ before_filter :authenticate_user!
3
+ before_filter :correct_user?, :except => [:index]
4
+
5
+ def index
6
+ @users = User.all
7
+ authorize User
8
+ end
9
+
10
+ def edit
11
+ @user = User.find(params[:id])
12
+ end
13
+
14
+ def update
15
+ @user = User.find(params[:id])
16
+ if @user.update_attributes(secure_params)
17
+ redirect_to @user
18
+ else
19
+ render :edit
20
+ end
21
+ end
22
+
23
+ def show
24
+ @user = User.find(params[:id])
25
+ authorize @user
26
+ end
27
+
28
+ private
29
+
30
+ def secure_params
31
+ params.require(:user).permit(:email)
32
+ end
33
+
34
+ end
@@ -0,0 +1,22 @@
1
+ # config/initializers/pundit.rb
2
+ # Extends the ApplicationController to add Pundit for authorization.
3
+ # Modify this file to change the behavior of a 'not authorized' error.
4
+ # Be sure to restart your server when you modify this file.
5
+ module PunditHelper
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ include Pundit
10
+ rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
11
+ end
12
+
13
+ private
14
+
15
+ def user_not_authorized
16
+ flash[:alert] = "Access denied."
17
+ redirect_to (request.referrer || root_path)
18
+ end
19
+
20
+ end
21
+
22
+ ApplicationController.send :include, PunditHelper
@@ -1,3 +1,3 @@
1
1
  module RailsAppsPages
2
- VERSION = "0.5.5"
2
+ VERSION = "0.5.6"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_apps_pages
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.5
4
+ version: 0.5.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Kehoe
@@ -60,9 +60,12 @@ files:
60
60
  - lib/generators/pages/about/templates/about.html.erb
61
61
  - lib/generators/pages/about/templates/about_page_spec.rb
62
62
  - lib/generators/pages/authorized/authorized_generator.rb
63
- - lib/generators/pages/authorized/templates/user_policy.rb
63
+ - lib/generators/pages/authorized/templates/devise/user_policy.rb
64
+ - lib/generators/pages/authorized/templates/devise/users_controller.rb
65
+ - lib/generators/pages/authorized/templates/omniauth/user_policy.rb
66
+ - lib/generators/pages/authorized/templates/omniauth/users_controller.rb
67
+ - lib/generators/pages/authorized/templates/pundit.rb
64
68
  - lib/generators/pages/authorized/templates/users/_user.html.erb
65
- - lib/generators/pages/authorized/templates/users_controller.rb
66
69
  - lib/generators/pages/home/home_generator.rb
67
70
  - lib/generators/pages/home/templates/home_page_spec.rb
68
71
  - lib/generators/pages/home/templates/index.html.erb