arpa 0.0.7 → 0.0.8

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: 931f0491ce2453cff1606cdc0f2cd5991a7921c6
4
- data.tar.gz: 6ed81530f8cc41943e1b9683c4676e60db6d697e
3
+ metadata.gz: 8ac4029644c373b6a7478a913a4e165eca5145ef
4
+ data.tar.gz: f4f47a1444f7cf74d61b09579816bafead29e91c
5
5
  SHA512:
6
- metadata.gz: 3a7cf1f28d58a0406abe0ff35ae3ab121bc8e209b5f807f287f2ca3e5309bbb5fc446a4799be6f034a7e6a80d61939c99b224ead2784a3d8dd26dbe5a7314068
7
- data.tar.gz: f0481f9001e29f74fa20db57c016270de9b4538a430691d67bfe727adda6190458d7216f691a169f78d2d84f8fd4b6ee14c8b9073fdea9e7234e12157bd7a822
6
+ metadata.gz: a6f282c6b5d8fff2b6ef47c6a16bcfb18ba7f53648de5119ceb06c35ed7baeeabfdfdc57c454fcaa6bc8457a199fc6dc047f8af3da41a0874760342907cea8c3
7
+ data.tar.gz: 0a3e5b3c15a7199bcda8a0188312b548cc8ceec87521e1ee28e8d39f2ff43f1e4a2d88b5b948b80fb1a47d2791ac0ae029deb3c3c7ae8456bcf221104f5c509a
data/.gitignore CHANGED
@@ -12,6 +12,7 @@
12
12
  *.so
13
13
  *.o
14
14
  *.a
15
+ *.gem
15
16
  mkmf.log
16
17
 
17
18
  # scm revert files
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  #Arpa
2
2
 
3
- Authorization Gem for Ruby and Ruby on Rails projects.
3
+ Arpa is an authorization library for Ruby or Ruby on Rails which restricts the accesses in controller and actions. Arpa will help you to customize all permissions you need dynamically.
4
4
 
5
5
  ## Installation
6
6
 
@@ -28,26 +28,55 @@ This command will create some files that are needed to run the Gem.
28
28
  |----------|:-------------:|
29
29
  | db/migrate/20140120201010_create_arpa_tables.rb | Migration to create the all **Arpa** tables in your database (your name will include a different timestamp) |
30
30
  | config/locales/arpa.en.yml | Locales to use in Arpa classes |
31
+
32
+ **Obs.:** The migration file will create a associate table between **Arpa::Profile** and **SomeModel**. By **default** the model is **User** as **users** table. The model to associate must exist in your Application before run that generate.
33
+
34
+ If you want a different Model to associate with Arpa::Profile you can pass some arguments:
35
+
36
+ $ rails generate arpa:install [ASSOCIATE_TABLE] [ASSOCIATE_PRIMARY_KEY]
37
+
38
+ ####Eg. 1:
39
+
40
+ $ rails generate arpa:install admins
41
+
42
+ That command will create the association with **admins** table with **admin_id** as foreign key.
43
+
44
+ ####Eg. 2:
45
+
46
+ $ rails generate arpa:install admins admin_custom_id
47
+
48
+ That command will create the association with **admins** table with **admin_custom_id** as foreign key.
49
+
50
+
51
+ After run the generate command, you need to run the migration to create all Arpa tables:
52
+
53
+ $ rake db:migrate
54
+
55
+ ---
56
+
57
+ Arpa can generate the *Controllers*, *Views*, *Stylesheet* and *Routes* to a basic CRUD for *resources*, *roles* and *profiles*. To do that you can run:
58
+
59
+ $ rails generate arpa:controllers
60
+
61
+ This command will create some files.
62
+
63
+ | File | Purpose |
64
+ |----------|:-------------:|
31
65
  | app/assets/stylesheets/arpa/arpa_accordion.scss | Basic stylesheet to use with Arpa views |
32
66
  | app/controllers/arpa/resources_controller.rb app/controllers/arpa/roles_controller.rb app/controllers/arpa/profiles_controller.rb | Controllers to use the CRUD actions for each one |
33
67
  | app/views/arpa/resources/ app/controllers/arpa/roles/ app/controllers/arpa/profiles/ | All views to use the CRUD actions for each controller above |
34
68
  | config/routes.rb | Will add all routes into this file with all resources of Arpa |
35
69
 
36
- After generate, you need to run the migration to create all Arpa tables:
37
-
38
- $ rake db:migrate
39
-
40
- **Obs.:** The migration file will create a associate table between **Profiles** and **Users** (the Users must exist in your Application before adding the Gem)
41
70
 
42
71
  ## Usage
43
72
 
44
- First of all you must create the Resources, Roles and Profiles (each is avaliable in the paths listed in a section bellow). After that you need associate **Profiles** with **User** (to do this, you need create by your own the associate form view, saving some profiles in some user). Done that you can use some Helpers generated by Arpa.
73
+ First of all you must create the Resources, Roles and Profiles (each is avaliable in the paths listed in a section bellow). After that you need associate **Arpa::Profile** with **SomeModel** (to do this, you need create by your own the associate form view, saving some profiles in some model). Done that you can use some Helpers generated by Arpa.
45
74
 
46
- ### Association between Profiles and Users
75
+ ### Association between Arpa::Profile and SomeModel
47
76
 
48
- You just need have a method called **:profile_ids** inside the User model. This method should return a list of ids from profiles associated in the user.
77
+ You just need have a method called **:profile_ids** inside the **SomeModel** model. This method should return a list of ids from profiles associated in the model.
49
78
 
50
- You just add a HBTM association in User model:
79
+ You just add a HBTM association in SomeModel model:
51
80
 
52
81
  ```ruby
53
82
  class User < ActiveRecord::Base
@@ -56,6 +85,16 @@ end
56
85
  ```
57
86
  With this you will be able to use the :profile_ids method.
58
87
 
88
+ If the Model name is different on database you need add the **foreign_key** option:
89
+
90
+ ```ruby
91
+ class User < ActiveRecord::Base
92
+ self.table_name = 'admins'
93
+
94
+ has_and_belongs_to_many :profiles, class_name: 'Arpa::Repositories::Profiles::RepositoryProfile', foreign_key: 'admin_id'
95
+ end
96
+ ```
97
+
59
98
  ### Controller helpers
60
99
 
61
100
  Arpa will create some helpers to use inside your controllers and views.
@@ -96,20 +135,62 @@ class ApplicationController < ActionController::Base
96
135
  before_filter :authorize_user
97
136
 
98
137
  def authorize_user
99
- controller = params[:controller]
100
- action = params[:action]
101
- redirect_to some_url unless has_access?(controller, action
138
+ controller = params[:controller]
139
+ action = params[:action]
140
+ redirect_to some_url unless has_access?(controller, action)
102
141
  end
103
142
 
104
143
  end
105
144
  ```
106
145
 
107
- **Obs.:** The **has_access?** method come from Controller Helper method which Arpa gem has been created.
146
+ **Obs. 1:** The **has_access?** method come from Controller Helper method which Arpa gem has been created.
147
+
148
+ **Obs. 2:** When you create the **before_filter** you probably wanna skip that callback in somes **controllers** (like login or devise controllers). To do this you need set the **skip_before_action** passing as parameter the name of before_filter method as you can see bellow:
149
+
150
+ ```ruby
151
+ skip_before_action :authorize_user
152
+ ```
153
+
154
+
155
+ ## Descriptions Locales for Arpa::Entities::Action
108
156
 
157
+ Arpa will use on **description** method from Arpa::Entities::Action a specific Locale.
158
+
159
+ You should create a locale file to print correctly the descriptions of the actions.
160
+
161
+ ####Eg.:
162
+
163
+ ```ruby
164
+ en:
165
+ entities:
166
+ resources:
167
+ users: #Here is the name of controller
168
+ actions:
169
+ description:
170
+ #Here is each action of the controller
171
+ index: 'List of Users'
172
+ show: 'Show of User'
173
+ new: 'Access to registration form of User'
174
+ edit: 'Access to change form of User'
175
+ create: 'Perform action registering of User'
176
+ update: 'Perform action update of User'
177
+ destroy: 'Perform action destroy of User'
178
+
179
+ ```
109
180
 
110
181
  ## Information
111
182
 
112
- After generate, you will be able to access some paths for each Controller created:
183
+ Arpa will add a new column called **is_arpa_admin** as boolean in the associate table with value **false** as default. You must set some user (creating a migration for example), with *is_arpa_admin* as **true** to navigate between the views without be catched by the filter verification.
184
+
185
+ If you want a **action** of some **Controller** pass without permission on *before_filter* callback. You just need start the name of action with underscode ('_'). For example:
186
+
187
+ ```ruby
188
+ def _some_free_action_which_not_need_permission
189
+ end
190
+ ```
191
+
192
+
193
+ The routes created by **arpa:controllers** generator will be able to access some paths for each Controller created:
113
194
 
114
195
  ```ruby
115
196
  generate_resources_and_actions_resources GET /resources/generate_resources_and_actions(.:format) arpa/resources#generate_resources_and_actions
@@ -22,12 +22,12 @@ module Arpa
22
22
  private
23
23
 
24
24
  def has_session_or_current_user?
25
- sess = try(:session)
26
- c_user = try(:current_user)
27
- return true if sess && c_user
25
+ verified_session = try(:session)
26
+ verified_current_user = try(:current_user)
27
+ return true if verified_session && verified_current_user
28
28
  log = Logger.new(STDOUT)
29
- log.warn("The ApplicationController must has a attribute or method 'session'") unless sess
30
- log.warn("The ApplicationController must has a attribute or method 'current_user'") unless c_user
29
+ log.warn("The ApplicationController must has a attribute or method 'session'") unless verified_session
30
+ log.warn("The ApplicationController must has a attribute or method 'current_user'") unless verified_current_user
31
31
  false
32
32
  end
33
33
 
@@ -16,7 +16,8 @@ module Arpa
16
16
  end
17
17
 
18
18
  def resource_name
19
- resource.name
19
+ return resource.name if resource
20
+ ''
20
21
  end
21
22
 
22
23
  def description
@@ -5,7 +5,7 @@ module Arpa
5
5
  attr_reader :permissions
6
6
 
7
7
  def initialize(actions)
8
- @actions = actions
8
+ @actions = actions
9
9
  @permissions = Array.new
10
10
  build_permissions
11
11
  end
@@ -10,11 +10,17 @@ module Arpa
10
10
  @id = attrs[:id]
11
11
  @name = attrs[:name]
12
12
  @description = attrs[:description]
13
- @role_ids = attrs[:role_ids] || []
14
- @roles = attrs[:roles] || []
13
+ @role_ids = default_value_to_nil_or_empty(attrs[:role_ids], [])
14
+ @roles = default_value_to_nil_or_empty(attrs[:roles], [])
15
15
  @created_at = attrs[:created_at]
16
16
  @updated_at = attrs[:updated_at]
17
- @removed = attrs[:removed] || false
17
+ @removed = default_value_to_nil_or_empty(attrs[:removed], false)
18
+ end
19
+
20
+ private
21
+ def default_value_to_nil_or_empty(attr_value, default_value)
22
+ return attr_value if attr_value.present?
23
+ default_value
18
24
  end
19
25
 
20
26
  end
@@ -12,7 +12,7 @@ module Arpa
12
12
  @name = attrs[:name]
13
13
  @created_at = attrs[:created_at]
14
14
  @updated_at = attrs[:updated_at]
15
- @actions = attrs[:actions] || []
15
+ @actions = attrs[:actions].present? ? attrs[:actions] : []
16
16
  end
17
17
 
18
18
  def build_correct_name
@@ -11,18 +11,24 @@ module Arpa
11
11
  @id = attrs[:id]
12
12
  @name = attrs[:name]
13
13
  @description = attrs[:description]
14
- @action_ids = attrs[:action_ids] || []
15
- @actions = attrs[:actions] || []
16
- @profiles = attrs[:profiles] || []
14
+ @action_ids = default_value_to_nil_or_empty(attrs[:action_ids], [])
15
+ @actions = default_value_to_nil_or_empty(attrs[:actions], [])
16
+ @profiles = default_value_to_nil_or_empty(attrs[:profiles], [])
17
17
  @created_at = attrs[:created_at]
18
18
  @updated_at = attrs[:updated_at]
19
- @removed = attrs[:removed] || false
19
+ @removed = default_value_to_nil_or_empty(attrs[:removed], false)
20
20
  end
21
21
 
22
22
  def has_profile?
23
23
  profiles.present?
24
24
  end
25
25
 
26
+ private
27
+ def default_value_to_nil_or_empty(attr_value, default_value)
28
+ return attr_value if attr_value.present?
29
+ default_value
30
+ end
31
+
26
32
  end
27
33
  end
28
34
  end
@@ -1,3 +1,3 @@
1
1
  module Arpa
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
@@ -1,17 +1,4 @@
1
1
  en:
2
- entities:
3
- resources:
4
- users:
5
- actions:
6
- description:
7
- index: 'List of Users'
8
- show: 'Show of User'
9
- new: 'Access to registration form of User'
10
- edit: 'Access to change form of User'
11
- create: 'Perform action registering of User'
12
- update: 'Perform action update of User'
13
- destroy: 'Perform action destroy of User'
14
-
15
2
  errors:
16
3
  format: ! '%{attribute} %{message}'
17
4
  messages:
@@ -1,18 +1,38 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Arpa::Entities::Action, type: :entity, fast: true do
4
- let(:resource) { double(name: 'users') }
5
-
6
4
  subject { Arpa::Entities::Action.new resource: resource, name: 'index'}
7
5
 
8
6
  describe 'getting description by action name and resource name' do
7
+ before do
8
+ I18n.backend.store_translations(:en, entities: { resources: { users: { actions: { description: { index: 'List of Users' } } } } } )
9
+ end
9
10
 
10
11
  context 'when resource.name is "users" and action.name is "index"' do
12
+ let(:resource) { double(name: 'users') }
11
13
 
12
14
  it 'description should be "List of Users"' do
13
15
  expect(subject.description).to eql "List of Users"
14
16
  end
15
17
  end
18
+ end
19
+
20
+ describe 'getting the resource name' do
21
+
22
+ context 'when resource is not nil' do
23
+ let(:resource) { double(name: 'users') }
24
+
25
+ it 'resource name should be the same of the attribute' do
26
+ expect(subject.resource_name).to eql 'users'
27
+ end
28
+ end
29
+
30
+ context 'when resource is nil' do
31
+ let(:resource) { nil }
32
+ it 'resource name should be empty' do
33
+ expect(subject.resource_name).to be_empty
34
+ end
35
+ end
16
36
 
17
37
  end
18
38
 
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Arpa::Entities::Permissions, type: :presenter, fast: true do
3
+ describe Arpa::Entities::Permissions, type: :entity, fast: true do
4
4
  let(:action_001) { double name: 'index', resource_name: 'users' }
5
5
  let(:action_002) { double name: 'show', resource_name: 'users' }
6
6
  let(:action_003) { double name: 'show', resource_name: 'users' }
@@ -34,26 +34,40 @@ describe Arpa::Entities::Permissions, type: :presenter, fast: true do
34
34
  end
35
35
 
36
36
  describe '#has_permission?' do
37
- let(:resource_name) { 'users' }
38
37
  let(:result) { subject.has_permission?(resource_name, action_name) }
39
38
 
40
- context 'when has' do
41
- let(:action_name) { 'index' }
39
+ context 'when pass a resource name with permission' do
40
+ let(:resource_name) { 'users' }
42
41
 
43
- it 'should return true' do
44
- expect(result).to be_truthy
42
+ context 'when pass an action with permission' do
43
+ let(:action_name) { 'index' }
44
+
45
+ it 'should return true' do
46
+ expect(result).to be_truthy
47
+ end
45
48
  end
46
49
 
50
+ context 'when pass an action with no permission' do
51
+ let(:action_name) { 'index_old' }
52
+
53
+ it 'should return false' do
54
+ expect(result).to be_falsey
55
+ end
56
+ end
47
57
  end
48
58
 
49
- context 'when has not' do
50
- let(:action_name) { 'index_old' }
59
+ context 'when pass a resource name with no permission' do
60
+ let(:resource_name) { 'users_2' }
51
61
 
52
- it 'should return false' do
53
- expect(result).to be_falsey
54
- end
62
+ context 'when pass some action' do
63
+ let(:action_name) { 'index' }
55
64
 
65
+ it 'should return false' do
66
+ expect(result).to be_falsey
67
+ end
68
+ end
56
69
  end
70
+
57
71
  end
58
72
 
59
73
  end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ describe Arpa::Entities::Profile, type: :entity, fast: true do
4
+
5
+ describe 'initializing' do
6
+
7
+ describe 'setting default values' do
8
+
9
+ context 'when pass some parameters as nil' do
10
+
11
+ it 'attribute :role_ids should be an empty Array' do
12
+ expect(subject.role_ids).to eq []
13
+ end
14
+
15
+ it 'attribute :role should be an empty Array' do
16
+ expect(subject.roles).to eq []
17
+ end
18
+
19
+ it 'attribute :removed should be false' do
20
+ expect(subject.removed).to be_falsey
21
+ end
22
+ end
23
+ end
24
+
25
+ context 'when pass some parameters as empty' do
26
+ let(:attrs) { {role_ids: '', roles: '', removed: ''} }
27
+ subject { Arpa::Entities::Profile.new attrs }
28
+
29
+ it 'attribute :role_ids should be an empty Array' do
30
+ expect(subject.role_ids).to eq []
31
+ end
32
+
33
+ it 'attribute :role should be an empty Array' do
34
+ expect(subject.roles).to eq []
35
+ end
36
+
37
+ it 'attribute :removed should be false' do
38
+ expect(subject.removed).to be_falsey
39
+ end
40
+ end
41
+
42
+ end
43
+
44
+ end
@@ -2,6 +2,29 @@ require 'spec_helper'
2
2
 
3
3
  describe Arpa::Entities::Resource, type: :entity, fast: true do
4
4
 
5
+ describe 'initializing' do
6
+
7
+ describe 'setting default values' do
8
+
9
+ context 'when pass :actions as nil' do
10
+
11
+ it 'attribute :actions should be an empty Array' do
12
+ expect(subject.actions).to eq []
13
+ end
14
+ end
15
+ end
16
+
17
+ context 'when pass :actions as empty' do
18
+ let(:attrs) { {actions: ''} }
19
+ subject { Arpa::Entities::Resource.new attrs }
20
+
21
+ it 'attribute :actions should be an empty Array' do
22
+ expect(subject.actions).to eq []
23
+ end
24
+ end
25
+
26
+ end
27
+
5
28
  describe '#build_correct_name' do
6
29
 
7
30
  before { subject.build_correct_name }
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+
3
+ describe Arpa::Entities::Role, type: :entity, fast: true do
4
+
5
+ describe 'initializing' do
6
+
7
+ describe 'setting default values' do
8
+
9
+ context 'when pass some parameters as nil' do
10
+
11
+ it 'attribute :action_ids should be an empty Array' do
12
+ expect(subject.action_ids).to eq []
13
+ end
14
+
15
+ it 'attribute :actions should be an empty Array' do
16
+ expect(subject.actions).to eq []
17
+ end
18
+
19
+ it 'attribute :profiles should be an empty Array' do
20
+ expect(subject.profiles).to eq []
21
+ end
22
+
23
+ it 'attribute :removed should be false' do
24
+ expect(subject.removed).to be_falsey
25
+ end
26
+ end
27
+ end
28
+
29
+ context 'when pass some parameters as empty' do
30
+ let(:attrs) { {action_ids: '', actions: '', profiles: '', removed: ''} }
31
+ subject { Arpa::Entities::Role.new attrs }
32
+
33
+ it 'attribute :action_ids should be an empty Array' do
34
+ expect(subject.action_ids).to eq []
35
+ end
36
+
37
+ it 'attribute :actions should be an empty Array' do
38
+ expect(subject.actions).to eq []
39
+ end
40
+
41
+ it 'attribute :profiles should be an empty Array' do
42
+ expect(subject.profiles).to eq []
43
+ end
44
+
45
+ it 'attribute :removed should be false' do
46
+ expect(subject.removed).to be_falsey
47
+ end
48
+ end
49
+
50
+ end
51
+
52
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arpa
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rachid Calazans
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-01 00:00:00.000000000 Z
11
+ date: 2016-05-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -156,9 +156,6 @@ files:
156
156
  - LICENSE.txt
157
157
  - README.md
158
158
  - Rakefile
159
- - arpa-0.0.2.gem
160
- - arpa-0.0.3.gem
161
- - arpa-0.0.4.gem
162
159
  - arpa.gemspec
163
160
  - lib/arpa.rb
164
161
  - lib/arpa/additions/resource.rb
@@ -249,7 +246,9 @@ files:
249
246
  - spec/lib/ar/data_mappers/role_mapper_spec.rb
250
247
  - spec/lib/ar/entities/action_spec.rb
251
248
  - spec/lib/ar/entities/permissions_spec.rb
249
+ - spec/lib/ar/entities/profile_spec.rb
252
250
  - spec/lib/ar/entities/resource_spec.rb
251
+ - spec/lib/ar/entities/role_spec.rb
253
252
  - spec/lib/ar/repositories/actions/finder_spec.rb
254
253
  - spec/lib/ar/repositories/base_spec.rb
255
254
  - spec/lib/ar/repositories/profiles/finder_spec.rb
@@ -310,7 +309,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
310
309
  version: '0'
311
310
  requirements: []
312
311
  rubyforge_project:
313
- rubygems_version: 2.4.5
312
+ rubygems_version: 2.4.6
314
313
  signing_key:
315
314
  specification_version: 4
316
315
  summary: Authorization Gem for Ruby and Ruby on Rails projects
@@ -327,7 +326,9 @@ test_files:
327
326
  - spec/lib/ar/data_mappers/role_mapper_spec.rb
328
327
  - spec/lib/ar/entities/action_spec.rb
329
328
  - spec/lib/ar/entities/permissions_spec.rb
329
+ - spec/lib/ar/entities/profile_spec.rb
330
330
  - spec/lib/ar/entities/resource_spec.rb
331
+ - spec/lib/ar/entities/role_spec.rb
331
332
  - spec/lib/ar/repositories/actions/finder_spec.rb
332
333
  - spec/lib/ar/repositories/base_spec.rb
333
334
  - spec/lib/ar/repositories/profiles/finder_spec.rb
Binary file
Binary file
Binary file