thecore_auth_commons 2.2.6 → 2.3.2

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
  SHA256:
3
- metadata.gz: 9e161c86e604e14243962d4919c383b3c975df439d6f8c1e181f2bd6f798117c
4
- data.tar.gz: 7b8763c79554933ae5ce2c9d067ff2eb82aaf472fa5ffa0a702d2cfced6a2daf
3
+ metadata.gz: f7820a25717ab5c4d9ff19a5dea6e7ab02c54a497348eef6d27e53dc459204f4
4
+ data.tar.gz: 22775ef09abefa8bbe2cbdbff7dc3d3e48a097153bd3af1f73ab584d149e9dab
5
5
  SHA512:
6
- metadata.gz: fc4279118d7e9a4f764c55a248d6244de35ef0394912b71797049055199a4b852fcb0aa26b57d384a5a23dd36f1196922678369ea216feb0f2c9a32e98f1b262
7
- data.tar.gz: ea961d67ef847e7e72760b82f2cdd66d03698faa5ada8b5f8a7ff413fe746e614052dc757f7c7e4e10a9705d832c9e6a0eb2fbaaea320781228d5e41f6e76f8e
6
+ metadata.gz: cbf7e8777abfdbea0deee34773d812a1dca5adc48dfcb3a5bacda983f489c18cd0cff586c6dad777ed59a1b0ccca3988296bddb12c9262d03bd4a20b3a44e163
7
+ data.tar.gz: ade3d82b997977aa4710e89ee9d77fda100b4c9ea2de2c812898afaa774c9936634b4b3218e1ee05ab11c8624113eb9d0e494b38f2b93d4274ca4812144cb178
@@ -44,6 +44,6 @@ class Ability
44
44
  ::Permission.joins(roles: :users).where(users: {id: user.id}).order(:id).each do |permission|
45
45
  # E.g. can :manage, :all
46
46
  self.send(permission.predicate.name.to_sym, permission.action.name.to_sym, (permission.target.name.classify.constantize rescue permission.target.name.to_sym))
47
- end
47
+ end unless user.blank?
48
48
  end
49
49
  end
data/app/models/user.rb CHANGED
@@ -33,11 +33,10 @@ class User < ApplicationRecord
33
33
  end
34
34
 
35
35
  def has_role? role
36
- roles.include? role
36
+ roles.include? role.to_s
37
37
  end
38
38
 
39
39
  def authenticate password
40
- puts "PASSWORD: #{password}"
41
40
  self&.valid_password?(password) ? self : nil
42
41
  end
43
42
 
@@ -1,32 +1,31 @@
1
1
  class CreatePermissions < ActiveRecord::Migration[6.0]
2
2
  def change
3
- @values = {
4
- predicates: %i[can cannot],
5
- actions: %i[manage create read update destroy],
6
- targets: ApplicationRecord.subclasses.map {|d| d.to_s.underscore}.to_a.unshift(:all)
7
- }
8
-
9
- def create_and_fill table
10
- create_table table do |t|
11
- t.string :name
12
- t.bigint :lock_version
3
+ # Predicates
4
+ create_table :predicates do |t|
5
+ t.string :name
6
+ t.bigint :lock_version
13
7
 
14
- t.timestamps
15
- end
16
- add_index table, :name, unique: true
17
- model = table.to_s.classify.constantize
18
- model.reset_column_information
19
- model.upsert_all @values[table].map { |p| {name: p, created_at: Time.now, updated_at: Time.now} }, unique_by: [:name]
8
+ t.timestamps
20
9
  end
21
-
22
- # Predicates
23
- create_and_fill :predicates
10
+ add_index :predicates, :name, unique: true
24
11
 
25
12
  # Actions
26
- create_and_fill :actions
13
+ create_table :actions do |t|
14
+ t.string :name
15
+ t.bigint :lock_version
16
+
17
+ t.timestamps
18
+ end
19
+ add_index :actions, :name, unique: true
27
20
 
28
21
  # Targets
29
- create_and_fill :targets
22
+ create_table :targets do |t|
23
+ t.string :name
24
+ t.bigint :lock_version
25
+
26
+ t.timestamps
27
+ end
28
+ add_index :targets, :name, unique: true
30
29
 
31
30
  create_table :permissions do |t|
32
31
  t.references :predicate, null: false, foreign_key: true
data/db/seeds.rb ADDED
@@ -0,0 +1,26 @@
1
+ puts "Loading ThecoreAuthCommons seeds"
2
+ email = ENV["ADMIN_EMAIL"].presence || "admin@example.com"
3
+ psswd = ENV["ADMIN_PASSWORD"].presence || "changeme"
4
+
5
+ u = User.find_or_initialize_by(email: email)
6
+ u.username = "Administrator"
7
+ u.password = u.password_confirmation = psswd
8
+ u.admin = true
9
+ u.save(validate: false)
10
+
11
+
12
+ @values = {
13
+ predicates: %i[can cannot],
14
+ actions: %i[manage create read update destroy],
15
+ targets: ApplicationRecord.subclasses.map {|d| d.to_s.underscore}.to_a.unshift(:all)
16
+ }
17
+
18
+ def fill table
19
+ model = table.to_s.classify.constantize
20
+ model.reset_column_information
21
+ model.upsert_all @values[table].map { |p| {name: p, created_at: Time.now, updated_at: Time.now} }, unique_by: [:name]
22
+ end
23
+
24
+ fill :predicates
25
+ fill :actions
26
+ fill :targets
@@ -2,3 +2,13 @@
2
2
  # task :thecore_auth_commons do
3
3
  # # Task goes here
4
4
  # end
5
+ namespace :thecore do
6
+ namespace :db do
7
+ desc "Load seeds from thecore engines seed files, it also runs rails db:seed as last action"
8
+ task seed: :environment do
9
+ Thecore::Base.thecore_engines.each { |engine| engine.send :load_seed }
10
+ Rake::Task["db:seed"].reenable
11
+ Rake::Task["db:seed"].invoke
12
+ end
13
+ end
14
+ end
@@ -8,3 +8,12 @@ require "thecore_auth_commons/engine"
8
8
  module ThecoreAuthCommons
9
9
  # Your code goes here...
10
10
  end
11
+
12
+ module Thecore
13
+ class Base
14
+ @@thecore_engines = []
15
+ def self.thecore_engines
16
+ @@thecore_engines
17
+ end
18
+ end
19
+ end
@@ -1,6 +1,11 @@
1
1
  module ThecoreAuthCommons
2
2
  class Engine < ::Rails::Engine
3
+ # https://stackoverflow.com/questions/12161376/rails-3-2-adding-seed-tasks-from-a-mountable-engine
4
+
3
5
  initializer 'thecore_auth_commons.add_to_migrations' do |app|
6
+ # Adds the list of Thecore Engines, so to manage seeds loading, i.e.:
7
+ # Thecore::Base.thecore_engines.each { |engine| engine.load_seed }
8
+ Thecore::Base.thecore_engines << self.class
4
9
  unless app.root.to_s.match root.to_s
5
10
  # APPEND TO MAIN APP MIGRATIONS FROM THIS GEM
6
11
  config.paths['db/migrate'].expanded.each do |expanded_path|
@@ -1,3 +1,3 @@
1
1
  module ThecoreAuthCommons
2
- VERSION = '2.2.6'.freeze
2
+ VERSION = "#{`git describe --tags $(git rev-list --tags --max-count=1)`.chomp}"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: thecore_auth_commons
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.6
4
+ version: 2.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabriele Tassoni
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-05-22 00:00:00.000000000 Z
11
+ date: 2021-03-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -114,13 +114,13 @@ files:
114
114
  - config/routes.rb
115
115
  - db/migrate/20200306143408_create_users.rb
116
116
  - db/migrate/20200306151046_add_admin_field_to_user.rb
117
- - db/migrate/20200306151541_add_first_admin_user.rb
118
117
  - db/migrate/20200306152740_create_roles.rb
119
118
  - db/migrate/20200306152816_create_role_users.rb
120
119
  - db/migrate/20200306153125_add_lock_version_to_user.rb
121
120
  - db/migrate/20200306153136_add_lock_version_to_role.rb
122
121
  - db/migrate/20200516215346_add_locked_to_user.rb
123
122
  - db/migrate/20200518082821_create_permissions.rb
123
+ - db/seeds.rb
124
124
  - lib/abilities/thecore_auth_commons.rb
125
125
  - lib/tasks/thecore_auth_commons_tasks.rake
126
126
  - lib/thecore_auth_commons.rb
@@ -1,60 +0,0 @@
1
- class AddFirstAdminUser < ActiveRecord::Migration[6.0]
2
- class User < ApplicationRecord
3
- # Include default devise modules. Others available are:
4
- # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
5
- devise :database_authenticatable, :trackable, :validatable
6
- # TODO: If it works, these must be added to another gem one which deal
7
- # more with sessions
8
- # devise :database_authenticatable
9
- # devise :rememberable
10
- # devise :trackable
11
- # devise :validatable
12
- # devise :timeoutable, timeout_in: 30.minutes
13
- # REFERENCES
14
- has_many :role_users, dependent: :destroy, inverse_of: :user
15
- has_many :roles, through: :role_users, inverse_of: :users
16
- # VALIDATIONS
17
- validates :email, uniqueness: { case_sensitive: false }, presence: true, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i }
18
- validates :password, presence: true, on: :create
19
- validates :password_confirmation, presence: true, on: :create
20
- validate :check_password_and_confirmation_equal
21
- validates_each :admin do |record, attr, value|
22
- # Don't want admin == false if the current user is the only admin
23
- record.errors.add(attr, I18n.t("validation.errors.cannot_unadmin_last_admin")) if record.admin_changed? && record.admin_was == true && User.where(admin: true).count == 1
24
- end
25
-
26
- def display_name
27
- email
28
- end
29
-
30
- def has_role? role
31
- roles.include? role
32
- end
33
-
34
- protected
35
-
36
- def check_password_and_confirmation_equal
37
- errors.add(:password, I18n.t("validation.errors.password_and_confirm_must_be_the_same")) unless password == password_confirmation
38
- end
39
- end
40
-
41
- def up
42
- email = "admin@example.com"
43
- User.reset_column_information
44
- u=User.find_or_initialize_by(email: email)
45
- psswd = SecureRandom.hex(5)
46
- u.password = psswd
47
- u.password_confirmation = psswd
48
- u.admin = true
49
- u.save!
50
- puts "\nPlease find generated initial admin password in .passwords file."
51
- File.open('.passwords', 'w') do |f|
52
- f.write(psswd)
53
- end
54
- end
55
-
56
- def down
57
- email = "admin@example.com"
58
- User.find_by(email: email).destroy
59
- end
60
- end