thecore_auth_commons 2.3.1 → 2.3.2
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 +4 -4
- data/db/migrate/20200518082821_create_permissions.rb +20 -21
- data/db/seeds.rb +26 -0
- data/lib/tasks/thecore_auth_commons_tasks.rake +10 -0
- data/lib/thecore_auth_commons.rb +9 -0
- data/lib/thecore_auth_commons/engine.rb +5 -0
- metadata +3 -3
- data/db/migrate/20200306151541_add_first_admin_user.rb +0 -60
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA256:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: f7820a25717ab5c4d9ff19a5dea6e7ab02c54a497348eef6d27e53dc459204f4
         | 
| 4 | 
            +
              data.tar.gz: 22775ef09abefa8bbe2cbdbff7dc3d3e48a097153bd3af1f73ab584d149e9dab
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: cbf7e8777abfdbea0deee34773d812a1dca5adc48dfcb3a5bacda983f489c18cd0cff586c6dad777ed59a1b0ccca3988296bddb12c9262d03bd4a20b3a44e163
         | 
| 7 | 
            +
              data.tar.gz: ade3d82b997977aa4710e89ee9d77fda100b4c9ea2de2c812898afaa774c9936634b4b3218e1ee05ab11c8624113eb9d0e494b38f2b93d4274ca4812144cb178
         | 
| @@ -1,32 +1,31 @@ | |
| 1 1 | 
             
            class CreatePermissions < ActiveRecord::Migration[6.0]
         | 
| 2 2 | 
             
              def change
         | 
| 3 | 
            -
                 | 
| 4 | 
            -
             | 
| 5 | 
            -
                   | 
| 6 | 
            -
                   | 
| 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 | 
            -
             | 
| 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 | 
            -
                 | 
| 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 | 
            -
                 | 
| 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
         | 
    
        data/lib/thecore_auth_commons.rb
    CHANGED
    
    
| @@ -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|
         | 
    
        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.3. | 
| 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: 2021-03- | 
| 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(validate: false)
         | 
| 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
         |