Module: Card::Set::All::Permissions

Extended by:
Card::Set
Defined in:
platypus/tmp/set/gem-defaults/mod007-permissions/all/permissions.rb

Overview

Set: All cards (Permissions)

Defined Under Namespace

Modules: ClassMethods

Constant Summary

Constants included from Helpers

Helpers::SET_PATTERN_TEST_REGEXP

Constants included from Event::Api

Event::Api::OPTIONS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Card::Set

reset

Methods included from I18nScope

#mod_name, #scope

Methods included from Registrar

#extended, #finalize_load, #process_base_modules, #register_set

Methods included from Helpers

#format_module, #format_modules, #method_missing, #modules, #pattern_code, #respond_to_missing?, #set_name_parts, #shortname, #underscored_name

Methods included from Card::Set::AdvancedApi

#assign_type, #attachment, #define_set_from_error, #ensure_set, #setting_opts, #stage_method

Methods included from Format

#before, #format, layout_method_name, #view, view_method_name, view_setting_method_name, wrapper_method_name

Methods included from Inheritance

#include_set, #include_set_formats

Methods included from Trait

#card_accessor, #card_reader, #card_writer, #require_field

Methods included from Event::Api

#event

Class Method Details

.source_locationObject



7
# File 'platypus/tmp/set/gem-defaults/mod007-permissions/all/permissions.rb', line 7

def self.source_location; "/Users/ethan/dev/decko/gem/mod/permissions/set/all/permissions.rb"; end

Instance Method Details

#anyone_can?(action) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'platypus/tmp/set/gem-defaults/mod007-permissions/all/permissions.rb', line 58

def anyone_can? action
  who_can(action).include? AnyoneID
end

#deny_because(why) ⇒ Object



102
103
104
105
# File 'platypus/tmp/set/gem-defaults/mod007-permissions/all/permissions.rb', line 102

def deny_because why
  @permission_errors << why if @permission_errors
  false
end

#each_field_as_bot(&block) ⇒ Object



181
182
183
184
185
186
187
188
# File 'platypus/tmp/set/gem-defaults/mod007-permissions/all/permissions.rb', line 181

def each_field_as_bot &block
  # find all cards with me as trunk and update their read_rule
  # (because of *type plus right)
  # skip if name is updated because will already be resaved
  Auth.as_bot do
    field_cards.compact.each(&block)
  end
end

#event: check_permissionsObject



29
30
31
32
33
# File 'platypus/tmp/set/gem-defaults/mod007-permissions/all/permissions.rb', line 29

event :check_permissions, :validate do
  track_permission_errors do
    ok? action_for_permission_check
  end
end

#event: set_field_read_rulesObject



14
15
16
# File 'platypus/tmp/set/gem-defaults/mod007-permissions/all/permissions.rb', line 14

event :set_field_read_rules, after: :set_read_rule, on: :update, changed: :type_id do
  each_field_as_bot(&:update_read_rule)
end

#event: set_read_ruleObject



8
9
10
11
12
# File 'platypus/tmp/set/gem-defaults/mod007-permissions/all/permissions.rb', line 8

event :set_read_rule, :store, on: :save, changed: %i[type_id name] do
  read_rule_id, read_rule_class = permission_rule_id_and_class(:read)
  self.read_rule_id = read_rule_id
  self.read_rule_class = read_rule_class
end

#event: update_read_ruleObject



18
19
20
21
22
23
24
25
26
27
# File 'platypus/tmp/set/gem-defaults/mod007-permissions/all/permissions.rb', line 18

event :update_read_rule do
  without_timestamps do
    reset_patterns # why is this needed?
    set_read_rule
    Card.where(id: id).update_all read_rule_id: read_rule_id,
                                  read_rule_class: read_rule_class
    expire :shared
    update_field_read_rules
  end
end

#left_permission_rule_id(action) ⇒ Object



74
75
76
77
78
# File 'platypus/tmp/set/gem-defaults/mod007-permissions/all/permissions.rb', line 74

def left_permission_rule_id action
  lcard = left_or_new(skip_virtual: true, skip_modules: true)
  action = :update if action == :create && lcard.real? && lcard.action != :create
  lcard.permission_rule_id action
end

#ok?(action) ⇒ Boolean

ok? and ok! are public facing methods to approve one action at a time

fetching: if the optional :trait parameter is supplied, it is passed to fetch and the test is perfomed on the fetched card, therefore:

 trait: :account      would fetch this card plus a tag codenamed :account
 trait: :roles, new: {} would initialize a new card with default ({}) options.

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
52
# File 'platypus/tmp/set/gem-defaults/mod007-permissions/all/permissions.rb', line 44

def ok? action
  @ok ||= {}
  aok = @ok[Auth.as_id] ||= {}
  if (cached = aok[action]).present?
    cached
  else
    aok[action] = send "ok_to_#{action}?"
  end
end

#ok_to_create?Boolean

Returns:

  • (Boolean)


127
128
129
130
131
132
# File 'platypus/tmp/set/gem-defaults/mod007-permissions/all/permissions.rb', line 127

def ok_to_create?
  return false unless permit :create
  return true if simple?

  %i[left right].find { |side| !ok_to_create_side side } ? false : true
end

#ok_to_create_side(side) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
# File 'platypus/tmp/set/gem-defaults/mod007-permissions/all/permissions.rb', line 134

def ok_to_create_side side
  # left is supercard; create permissions will get checked there.
  return true if side == :left && superleft

  part_card = send side, new: {}
  # if no card, there must be other errors
  return true unless part_card&.new_card? && !part_card.ok?(:create)

  deny_because you_cant("create #{part_card.name}")
  false
end

#ok_to_delete?Boolean

Returns:

  • (Boolean)


162
163
164
# File 'platypus/tmp/set/gem-defaults/mod007-permissions/all/permissions.rb', line 162

def ok_to_delete?
  permit :delete
end

#ok_to_read?Boolean

Returns:

  • (Boolean)


146
147
148
149
150
151
152
153
# File 'platypus/tmp/set/gem-defaults/mod007-permissions/all/permissions.rb', line 146

def ok_to_read?
  return true if Auth.always_ok?

  self.read_rule_id ||= permission_rule_id :read
  return true if Auth.as_card.read_rules_hash[read_rule_id]

  deny_because you_cant "read this"
end

#ok_to_update?Boolean

Returns:

  • (Boolean)


155
156
157
158
159
160
# File 'platypus/tmp/set/gem-defaults/mod007-permissions/all/permissions.rb', line 155

def ok_to_update?
  return false unless permit(:update)
  return true unless type_id_changed? && !permitted?(:create)

  deny_because you_cant("change to this type (need create permission)")
end

#permission_rule_card(action) ⇒ Object



80
81
82
# File 'platypus/tmp/set/gem-defaults/mod007-permissions/all/permissions.rb', line 80

def permission_rule_card action
  Card.fetch permission_rule_id(action)
end

#permission_rule_id(action) ⇒ Object



62
63
64
65
66
67
68
# File 'platypus/tmp/set/gem-defaults/mod007-permissions/all/permissions.rb', line 62

def permission_rule_id action
  if compound? && rule(action).match?(/^\[?\[?_left\]?\]?$/)
    left_permission_rule_id action
  else
    rule_card_id action
  end
end

#permission_rule_id_and_class(action) ⇒ Object



70
71
72
# File 'platypus/tmp/set/gem-defaults/mod007-permissions/all/permissions.rb', line 70

def permission_rule_id_and_class action
  [permission_rule_id(action), direct_rule_card(action).rule_class_name]
end

#permit(action, verb = nil) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
# File 'platypus/tmp/set/gem-defaults/mod007-permissions/all/permissions.rb', line 114

def permit action, verb=nil
  # not called by ok_to_read?
  if Card.config.read_only
    deny_because "Currently in read-only mode"
    return false
  end

  return true if permitted? action

  verb ||= action.to_s
  deny_because you_cant("#{verb} #{name.present? ? name : 'this'}")
end

#permitted?(action) ⇒ Boolean

Returns:

  • (Boolean)


107
108
109
110
111
112
# File 'platypus/tmp/set/gem-defaults/mod007-permissions/all/permissions.rb', line 107

def permitted? action
  return false if Card.config.read_only # :read does not call #permit
  return true if Auth.always_ok?

  Auth.as_card.among? who_can(action)
end

#repair_permissions!Object



190
191
192
193
# File 'platypus/tmp/set/gem-defaults/mod007-permissions/all/permissions.rb', line 190

def repair_permissions!
  rule_id, rule_class = permission_rule_id_and_class :read
  update_columns read_rule_id: rule_id, read_rule_class: rule_class
end

#require_permission_rule!(rule_id, action) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'platypus/tmp/set/gem-defaults/mod007-permissions/all/permissions.rb', line 84

def require_permission_rule! rule_id, action
  return if rule_id

  # RULE missing.  should not be possible.
  # generalize this to handling of all required rules
  errors.add :permission_denied,
             t(:permission_error_no_action_rule, action: action, name: name)
  raise Card::Error::PermissionDenied, self
end

#rule_class_nameObject



94
95
96
# File 'platypus/tmp/set/gem-defaults/mod007-permissions/all/permissions.rb', line 94

def rule_class_name
  trunk.type_id == SetID ? name.trunk_name.tag : nil
end

#update_field_read_rulesObject

don’t know why we introduced this but we have to preserve read rules to make delete acts visible in recent changes -pk event :clear_read_rule, :store, on: :delete do self.read_rule_id = self.read_rule_class = nil end



173
174
175
176
177
178
179
# File 'platypus/tmp/set/gem-defaults/mod007-permissions/all/permissions.rb', line 173

def update_field_read_rules
  return unless type_id_changed? || read_rule_id_changed?

  each_field_as_bot do |field|
    field.update_read_rule if field.rule(:read) == "_left"
  end
end

#who_can(action) ⇒ Object



54
55
56
# File 'platypus/tmp/set/gem-defaults/mod007-permissions/all/permissions.rb', line 54

def who_can action
  permission_rule_card(action).item_cards.map(&:id)
end

#you_cant(what) ⇒ Object



98
99
100
# File 'platypus/tmp/set/gem-defaults/mod007-permissions/all/permissions.rb', line 98

def you_cant what
  "You don't have permission to #{what}"
end