check_permission 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 13be71cda261e5fec78b8a0733c048b72cb7c2bf
4
+ data.tar.gz: fa8c7df9e0aa27bef3474231fe514298c736028e
5
+ SHA512:
6
+ metadata.gz: 625f60b22333f334570d1abbf0a2a7e973dafe43f3871d486a8f42e8ec4b605447a9fd0b460b9f0a0a17499b7b2a60a4008d25466c0585c3c8e1b9167e90f1c3
7
+ data.tar.gz: 081346a0909b3db218c7e21c38b76675bffb74399c0362fcb05779918d099914c5529255e6d9d2efa154471c903cfe5409122129b94e2f11ddf1cb83638fe180
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.3
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in check_permission.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 sunil
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,83 @@
1
+ # CheckPermission
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/check_permission`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'check_permission'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install check_permission
22
+
23
+ ## Usage
24
+
25
+ rails g check_permission:install
26
+
27
+ It will create the permission.rb . Define the model in permission.rb, applying the permission for
28
+ ::Permission_for = [Post]
29
+
30
+ rails g check_permission User
31
+
32
+ It will create the migrations for the user and permission and it will create the models for the same.
33
+
34
+ Create form for assinging the permissions.
35
+ in UsersController
36
+
37
+ def new
38
+ @user = User.new
39
+ Permission_for.each do |model|
40
+ @user.permissions.build(resource_name: model)
41
+ end
42
+ end
43
+
44
+ In _form.html.erb
45
+
46
+ <%= form_for @user do |f|%
47
+ -- stuff --
48
+ <%= f.fields_for :permissions do |p|%>
49
+ <tr>
50
+ <%=p.hidden_field :resource_name%>
51
+ <td><%=p.object.resource_name%></td>
52
+ <td><%=p.check_box :is_read%></td>
53
+ <td><%=p.check_box :is_create%></td>
54
+ <td><%=p.check_box :is_update%></td>
55
+ <td><%=p.check_box :is_destroy%></td>
56
+
57
+ </tr>
58
+ <% end %>
59
+ <%end%
60
+
61
+ Call in controller or view has_permission
62
+
63
+ def edit
64
+ -- do stuff --
65
+ end if has_permission
66
+
67
+ has_permission work with current_user (logged_in) only.
68
+
69
+ ## Development
70
+
71
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
72
+
73
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
74
+
75
+ ## Contributing
76
+
77
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/check_permission.
78
+
79
+
80
+ ## License
81
+
82
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
83
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "check_permission"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'check_permission/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "check_permission"
8
+ spec.version = CheckPermission::VERSION
9
+ spec.authors = ["sunil"]
10
+ spec.email = ["sunil.kumar.14@netsolutionsindia.com"]
11
+
12
+ spec.summary = %q{To set the action based permission for the user and check the has_permission for the action.}
13
+ spec.description = %q{Write a longer description or delete this line.}
14
+ spec.homepage = "https://github.com/sunil-netsol/permission"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata['allowed_push_host'] = "Set to 'http://mygemserver.com'"
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ # end
24
+
25
+ spec.files = `git ls-files`.split("\n").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.10"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_development_dependency "rspec"
33
+ end
@@ -0,0 +1,3 @@
1
+ module CheckPermission
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,35 @@
1
+ require "check_permission/version"
2
+
3
+ module CheckPermission
4
+ def has_permission params
5
+ p "in check permission with #{params}"
6
+ resource = params[:controller].singularize.camelize
7
+ action = params[:action]
8
+ begin
9
+ permission = current_user.permissions
10
+ rescue
11
+ return raise "current_user is nil"
12
+ else
13
+ return result permission, resource
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def result permission, resource
20
+ permission = current_user.permissions.where(resource_name: resource).last
21
+ return false if permission.nil?
22
+ case action.to_sym
23
+ when :index
24
+ return permission.read_only
25
+ when :create
26
+ return permission.create_only
27
+ when :update
28
+ return permission.update_only
29
+ when :destroy, :delete
30
+ return permission.destroy_only
31
+ else
32
+ return false
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,47 @@
1
+ require 'rails/generators/active_record'
2
+ require 'generators/check_permission/orm_helpers'
3
+ module ActiveRecord
4
+ module Generators
5
+ class CheckPermissionGenerator < ActiveRecord::Generators::Base
6
+ argument :attributes, type: :array, default: [], banner: "field:type field:type"
7
+ include CheckPermission::Generators::OrmHelpers
8
+ source_root File.expand_path("../templates", __FILE__)
9
+ def copy_permission_migration
10
+ if (behavior == :invoke && model_exists?) || (behavior == :revoke && migration_exists?(table_name))
11
+ migration_template "migration_existing.rb", "db/migrate/add_permission_to_#{table_name}.rb"
12
+ else
13
+ migration_template "migration.rb", "db/migrate/permission_create_#{table_name}.rb"
14
+ end
15
+ end
16
+ def generate_model
17
+ invoke "active_record:model", [name], migration: false unless model_exists? && behavior == :invoke
18
+ end
19
+
20
+ def generate_model_permission
21
+ puts "i am in permission model #{name}"
22
+ permission = "Permission"
23
+ Rails::Generators.invoke("active_record:model", [permission,"is_read:boolean", "is_update:boolean",
24
+ "is_create:boolean", "is_destroy:boolean", "resource_name:string", "#{table_name.singularize}:references"], {migration: true, timestamps: true})
25
+ end
26
+
27
+ def inject_permission_content
28
+ content = model_contents
29
+ class_path = if namespaced?
30
+ class_name.to_s.split("::")
31
+ else
32
+ [class_name]
33
+ end
34
+ indent_depth = class_path.size - 1
35
+ content = content.split("\n").map { |line| " " * indent_depth + line } .join("\n") << "\n"
36
+ inject_into_class(model_path, class_path.last, content) if model_exists?
37
+ end
38
+
39
+ def migration_data
40
+ <<RUBY
41
+ t.string :email
42
+ t.string :name
43
+ RUBY
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,17 @@
1
+ class AddPermissionTo<%= table_name.camelize %> < ActiveRecord::Migration
2
+ def self.up
3
+ change_table(:<%= table_name %>) do |t|
4
+ <%= migration_data -%>
5
+ <% attributes.each do |attribute| -%>
6
+ t.<%= attribute.type %> :<%= attribute.name %>
7
+ <% end -%>
8
+ # Uncomment below if timestamps were not included in your original model.
9
+ # t.timestamps null: false
10
+ end
11
+ end
12
+ def self.down
13
+ # By default, we don't want to make any assumption about how to roll back a migration when your
14
+ # model already existed. Please edit below which fields you would like to remove in this migration.
15
+ raise ActiveRecord::IrreversibleMigration
16
+ end
17
+ end
@@ -0,0 +1,11 @@
1
+ class PermissionCreate<%= table_name.camelize %> < ActiveRecord::Migration
2
+ def change
3
+ create_table(:<%= table_name %>) do |t|
4
+ <%= migration_data -%>
5
+ <% attributes.each do |attribute| -%>
6
+ t.<%= attribute.type %> :<%= attribute.name %>
7
+ <% end -%>
8
+ t.timestamps null: false
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,16 @@
1
+ require 'rails/generators/named_base'
2
+ module CheckPermission
3
+ module Generators
4
+ class CheckPermissionGenerator < Rails::Generators::NamedBase
5
+ include Rails::Generators::ResourceHelpers
6
+ puts "pemission base name estart"
7
+ namespace "check_permission"
8
+ source_root File.expand_path("../templates", __FILE__)
9
+ desc "Generates a model with the given NAME (if one does not exist) with permission " <<
10
+ "configuration plus a migration file."
11
+ hook_for :orm
12
+ class_option :routes, desc: "Generate routes", type: :boolean, default: true
13
+ puts "pemission base name end"
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,11 @@
1
+ require 'rails/generators/base'
2
+ require 'securerandom'
3
+ module CheckPermission
4
+ class InstallGenerator < Rails::Generators::Base
5
+ source_root File.expand_path("../templates", __FILE__)
6
+ class_option :orm
7
+ def create_initializer_file
8
+ create_file Rails.root.join("config", "initializers", "permission.rb"), "#::Permission_for = [Page, Category]"
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,39 @@
1
+ module CheckPermission
2
+ module Generators
3
+ module OrmHelpers
4
+ def model_contents
5
+ buffer = <<-CONTENT
6
+ # Add has_many here for the permission for.
7
+ has_many :permissions
8
+ accepts_nested_attributes_for :permissions
9
+ CONTENT
10
+ buffer += <<-CONTENT
11
+ # Setup accessible (or protected) attributes for your model
12
+ CONTENT
13
+ buffer
14
+ end
15
+ private
16
+ def permission_model_exists?
17
+ File.exists?(File.join(destination_root, permission_model_path))
18
+ end
19
+
20
+ def model_exists?
21
+ File.exists?(File.join(destination_root, model_path))
22
+ end
23
+
24
+ def migration_exists?(table_name)
25
+ Dir.glob("#{File.join(destination_root, migration_path)}/[0-9]*_*.rb").grep(/\d+_#{table_name}.rb$/).first
26
+ end
27
+ def migration_path
28
+ @migration_path ||= File.join("db", "migrate")
29
+ end
30
+ def model_path
31
+ @model_path ||= File.join("app", "models", "#{file_path}.rb")
32
+ end
33
+
34
+ def permission_model_path
35
+ @permission_model_path ||= File.join("app", "models", "permission.rb")
36
+ end
37
+ end
38
+ end
39
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: check_permission
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - sunil
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-09-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Write a longer description or delete this line.
56
+ email:
57
+ - sunil.kumar.14@netsolutionsindia.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - check_permission.gemspec
72
+ - lib/check_permission.rb
73
+ - lib/check_permission/version.rb
74
+ - lib/generators/active_record/check_permission_generator.rb
75
+ - lib/generators/active_record/templates/existing_migration.rb
76
+ - lib/generators/active_record/templates/migration.rb
77
+ - lib/generators/check_permission/check_permission_generator.rb
78
+ - lib/generators/check_permission/install_generator.rb
79
+ - lib/generators/check_permission/orm_helpers.rb
80
+ homepage: https://github.com/sunil-netsol/permission
81
+ licenses:
82
+ - MIT
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.4.8
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: To set the action based permission for the user and check the has_permission
104
+ for the action.
105
+ test_files: []