diff options
| -rw-r--r-- | app/models/node.rb | 1 | ||||
| -rw-r--r-- | app/models/permission.rb | 13 | ||||
| -rw-r--r-- | app/models/user.rb | 55 | ||||
| -rw-r--r-- | db/migrate/20260731123346_drop_permissions.rb | 17 | ||||
| -rw-r--r-- | test/fixtures/permissions.yml | 7 | ||||
| -rw-r--r-- | test/models/permission_test.rb | 8 |
6 files changed, 17 insertions, 84 deletions
diff --git a/app/models/node.rb b/app/models/node.rb index c8f9a210..1823daa4 100644 --- a/app/models/node.rb +++ b/app/models/node.rb | |||
| @@ -18,7 +18,6 @@ class Node < ApplicationRecord | |||
| 18 | # them -- this dependent: :destroy is their only cleanup on node destroy. | 18 | # them -- this dependent: :destroy is their only cleanup on node destroy. |
| 19 | belongs_to :autosave, :class_name => "Page", :foreign_key => :autosave_id, :dependent => :destroy, optional: true | 19 | belongs_to :autosave, :class_name => "Page", :foreign_key => :autosave_id, :dependent => :destroy, optional: true |
| 20 | 20 | ||
| 21 | has_many :permissions, :dependent => :destroy | ||
| 22 | has_many :events, :dependent => :destroy | 21 | has_many :events, :dependent => :destroy |
| 23 | belongs_to :lock_owner, :class_name => "User", :foreign_key => :locking_user_id, optional: true | 22 | belongs_to :lock_owner, :class_name => "User", :foreign_key => :locking_user_id, optional: true |
| 24 | 23 | ||
diff --git a/app/models/permission.rb b/app/models/permission.rb deleted file mode 100644 index 1383a4b8..00000000 --- a/app/models/permission.rb +++ /dev/null | |||
| @@ -1,13 +0,0 @@ | |||
| 1 | class Permission < ApplicationRecord | ||
| 2 | # Validations | ||
| 3 | validates_presence_of :user_id, :node_id, :granted | ||
| 4 | validates_inclusion_of :granted, :in => [true, false] | ||
| 5 | |||
| 6 | # Associations | ||
| 7 | belongs_to :user | ||
| 8 | belongs_to :node | ||
| 9 | |||
| 10 | # Named scopes | ||
| 11 | scope :for_node, ->(node) { where('node_id = ?', (node.is_a?(Node) ? node.id : node)) } | ||
| 12 | scope :for_user, ->(user) { where('user_id = ?', (user.is_a?(User) ? user.id : user)) } | ||
| 13 | end | ||
diff --git a/app/models/user.rb b/app/models/user.rb index 4d712f6c..e1eff059 100644 --- a/app/models/user.rb +++ b/app/models/user.rb | |||
| @@ -9,9 +9,6 @@ class User < ApplicationRecord | |||
| 9 | include Authentication | 9 | include Authentication |
| 10 | include Authentication::ByPassword | 10 | include Authentication::ByPassword |
| 11 | 11 | ||
| 12 | # Associations | ||
| 13 | has_many :permissions | ||
| 14 | |||
| 15 | # Validations | 12 | # Validations |
| 16 | validates_presence_of :login | 13 | validates_presence_of :login |
| 17 | validates_length_of :login, :within => 1..40 | 14 | validates_length_of :login, :within => 1..40 |
| @@ -62,46 +59,6 @@ class User < ApplicationRecord | |||
| 62 | write_attribute :email, (value ? value.downcase : nil) | 59 | write_attribute :email, (value ? value.downcase : nil) |
| 63 | end | 60 | end |
| 64 | 61 | ||
| 65 | # Permission stuff | ||
| 66 | |||
| 67 | def grant(node) | ||
| 68 | set_permission(true, node) | ||
| 69 | end | ||
| 70 | |||
| 71 | def revoke(node) | ||
| 72 | set_permission(false, node) | ||
| 73 | end | ||
| 74 | |||
| 75 | def inherit(node) | ||
| 76 | permission = self.permissions.for_node(node).first | ||
| 77 | permission.destroy if permission | ||
| 78 | end | ||
| 79 | |||
| 80 | def get_permission_for(node) | ||
| 81 | permissions = {} | ||
| 82 | self.permissions.for_node(node).each do |permission| | ||
| 83 | permissions[permission.identifier.to_sym] = permission.granted | ||
| 84 | end | ||
| 85 | permissions | ||
| 86 | end | ||
| 87 | |||
| 88 | # Checks for permission on the node and if necessary ascends the | ||
| 89 | # nodetree until permission is found or returns false if it is not found | ||
| 90 | # at all. | ||
| 91 | def has_permission?(node) | ||
| 92 | node_permission = self.permissions.for_node(node) | ||
| 93 | return node_permission unless node_permission.nil? | ||
| 94 | |||
| 95 | node.ancestors.reverse.each do |p| | ||
| 96 | local_permission = self.get_permissions_for(p)[identifier] | ||
| 97 | unless local_permission.nil? | ||
| 98 | return local_permission | ||
| 99 | end | ||
| 100 | end | ||
| 101 | |||
| 102 | return false | ||
| 103 | end | ||
| 104 | |||
| 105 | def is_admin? | 62 | def is_admin? |
| 106 | !!admin | 63 | !!admin |
| 107 | end | 64 | end |
| @@ -176,16 +133,4 @@ class User < ApplicationRecord | |||
| 176 | end | 133 | end |
| 177 | true | 134 | true |
| 178 | end | 135 | end |
| 179 | |||
| 180 | private | ||
| 181 | |||
| 182 | def set_permission(granted, node) | ||
| 183 | permission = self.permissions.for_node(node).first | ||
| 184 | if permission | ||
| 185 | permission.granted = granted | ||
| 186 | else | ||
| 187 | self.permissions.create!( :node => node, | ||
| 188 | :granted => granted ) | ||
| 189 | end | ||
| 190 | end | ||
| 191 | end | 136 | end |
diff --git a/db/migrate/20260731123346_drop_permissions.rb b/db/migrate/20260731123346_drop_permissions.rb new file mode 100644 index 00000000..6ea7d2f0 --- /dev/null +++ b/db/migrate/20260731123346_drop_permissions.rb | |||
| @@ -0,0 +1,17 @@ | |||
| 1 | class DropPermissions < ActiveRecord::Migration[8.1] | ||
| 2 | def up | ||
| 3 | drop_table :permissions | ||
| 4 | end | ||
| 5 | |||
| 6 | # Reversible for form's sake -- the table was empty and every write path | ||
| 7 | # in the model was broken, so there is nothing to restore. | ||
| 8 | def down | ||
| 9 | create_table :permissions, :id => :serial do |t| | ||
| 10 | t.boolean :granted | ||
| 11 | t.integer :node_id | ||
| 12 | t.integer :user_id | ||
| 13 | t.datetime :created_at, :precision => nil | ||
| 14 | t.datetime :updated_at, :precision => nil | ||
| 15 | end | ||
| 16 | end | ||
| 17 | end | ||
diff --git a/test/fixtures/permissions.yml b/test/fixtures/permissions.yml deleted file mode 100644 index 5bf02933..00000000 --- a/test/fixtures/permissions.yml +++ /dev/null | |||
| @@ -1,7 +0,0 @@ | |||
| 1 | # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html | ||
| 2 | |||
| 3 | # one: | ||
| 4 | # column: value | ||
| 5 | # | ||
| 6 | # two: | ||
| 7 | # column: value | ||
diff --git a/test/models/permission_test.rb b/test/models/permission_test.rb deleted file mode 100644 index 08fcc0be..00000000 --- a/test/models/permission_test.rb +++ /dev/null | |||
| @@ -1,8 +0,0 @@ | |||
| 1 | require 'test_helper' | ||
| 2 | |||
| 3 | class PermissionTest < ActiveSupport::TestCase | ||
| 4 | # Replace this with your real tests. | ||
| 5 | test "the truth" do | ||
| 6 | assert true | ||
| 7 | end | ||
| 8 | end | ||
