class Object

Public Instance Methods

abort_unless(value, error_key) click to toggle source
# File set/self/signin.rb, line 67
def abort_unless value, error_key
  abort :failure, t("account_#{error_key}") unless value
end
account() click to toggle source
# File set/abstract/accountable.rb, line 1
def account
  fetch :account, new: {}
end
account_card() click to toggle source
# File set/abstract/account_field.rb, line 3
def account_card
  left
end
account_content_items() click to toggle source
# File set/abstract/accountable.rb, line 49
def account_content_items
  [["Created", :created],
   ["Edited", :edited]]
end
account_details_items() click to toggle source
# File set/abstract/accountable.rb, line 35
def account_details_items
  [
    ["Email and Password", :account,
     { path: { slot: { hide: %i[help_link bridge_link] } } }],
    ["Roles", :roles,
     { path:  { view: :content_with_edit_button } }],
    ["Notifications", :follow],
    ["API", :account,
     { path: { view: :api_key,
               items: { view: :content },
               slot: { hide: %i[help_link bridge_link] } } }]
  ]
end
account_email() click to toggle source
# File set/type_plus_right/user/email.rb, line 11
def account_email
  left&.account&.email
end
account_field_nest(field, title) click to toggle source
# File set/right/account/views.rb, line 47
def account_field_nest field, title
  field_nest field, title: title, view: :labeled
  # edit: :inline, hide: [:help_link, :bridge_link]
end
account_for(email) click to toggle source
# File set/self/signin.rb, line 79
def account_for email
  Auth.find_account_by_email email
end
account_formgroups() click to toggle source
# File set/abstract/accountable.rb, line 29
def account_formgroups
  Auth.as_bot do
    subformat(card.account)._render :content_formgroups, structure: true
  end
end
account_lines() click to toggle source
# File set/type/signup/views.rb, line 46
def account_lines
  if card.account
    verification_lines
  else
    [t(:account_missing_account)]
  end
end
account_subfield() click to toggle source
# File set/type/signup.rb, line 38
def account_subfield
  subfield(:account) || add_subfield(:account)
end
accounted() click to toggle source
# File set/right/account.rb, line 9
def accounted
  left
end
accounted_id() click to toggle source
# File set/right/account.rb, line 13
def accounted_id
  left_id
end
activatable() { || ... } click to toggle source

EVENT HELPERS

# File set/right/account/events.rb, line 40
def activatable
  abort :failure, "no field manipulation mid-activation" if subcards.present?
  # above is necessary because activation uses super user (Decko Bot),
  # so allowing subcards would be unsafe
  yield
end
activate_accounted() click to toggle source
# File set/type/signup.rb, line 13
def activate_accounted
  self.type_id = Card.default_accounted_type_id
end
all_enabled_roles() click to toggle source
# File set/all/account.rb, line 64
def all_enabled_roles
  @all_active_roles ||= (id == AnonymousID ? [] : enabled_role_ids)
end
all_roles() click to toggle source
# File set/all/account.rb, line 68
def all_roles
  @all_roles ||= (id == AnonymousID ? [] : fetch_roles)
end
among?(ok_ids) click to toggle source
# File set/all/account.rb, line 15
def among? ok_ids
  ok_ids.any? do |ok_id|
    ok_id == AnyoneID ||
      (ok_id == AnyoneWithRoleID && all_enabled_roles.size > 1) ||
      parties.member?(ok_id)
  end
end
approvable() { || ... } click to toggle source
# File set/type/signup.rb, line 48
def approvable
  if can_approve?
    yield
  else
    abort :failure, "illegal approval" # raise permission denied?
  end
end
authenticate_and_signin(email, pword) click to toggle source
# File set/self/signin.rb, line 56
def authenticate_and_signin email, pword
  return unless (account = Auth.authenticate email, pword)

  Auth.signin account.left_id
end
authenticate_or_abort(email, pword) click to toggle source
# File set/self/signin.rb, line 50
def authenticate_or_abort email, pword
  abort_unless email, :email_missing
  abort_unless pword, :password_missing
  authenticate_and_signin(email, pword) || failed_signin(email)
end
autocomplete?() click to toggle source
# File set/right/password.rb, line 47
def autocomplete?
  return "on" if @parent && @parent.card.name == "*signin+*account" # HACK

  "off"
end
blank_email?() click to toggle source
# File set/self/signin.rb, line 91
def blank_email?
  return false if email_from_subfield.present?

  error_on :email, :error_blank
end
can_approve?() click to toggle source
# File set/type/signup.rb, line 9
def can_approve?
  Card.new(type_id: Card.default_accounted_type_id).ok? :create
end
changes_visible?(act) click to toggle source
# File set/right/account.rb, line 28
def changes_visible? act
  act.actions_affecting(act.card).each do |action|
    return true if action.card.ok? :read
  end
  false
end
clear_roles() click to toggle source
# File set/all/account.rb, line 47
def clear_roles
  @parties = @all_roles = @all_active_roles = @read_rules = nil
end
content_email() click to toggle source
# File set/type_plus_right/user/email.rb, line 7
def content_email
  content if real?
end
current_account?() click to toggle source
# File set/abstract/accountable.rb, line 9
def current_account?
  id && Auth.current_id == id
end
default_account_status() click to toggle source
# File set/abstract/accountable.rb, line 5
def default_account_status
  "active"
end
default_bridge_tab() click to toggle source
Calls superclass method
# File set/abstract/accountable.rb, line 14
def default_bridge_tab
  card.current_account? ? :account_tab : super
end
disabled?() click to toggle source
# File set/type/role.rb, line 1
def disabled?
  Auth.current&.fetch(:disabled_roles)&.item_ids&.include? id
end
edit_success() click to toggle source
# File set/self/signin.rb, line 204
def edit_success
  { view: :reset_password_success }
end
edit_view_hidden() click to toggle source
# File set/self/signin.rb, line 200
def edit_view_hidden
  hidden_tags card: { trigger: :send_reset_password_token }
end
email_from_subfield() click to toggle source
# File set/self/signin.rb, line 38
def email_from_subfield
  @email_from_subfield ||= subfield(:email)&.content
end
email_required?() click to toggle source
# File set/right/email.rb, line 32
def email_required?
  !left.system?
end
enabled_role_ids() click to toggle source
# File set/all/account.rb, line 72
def enabled_role_ids
  with_enabled_roles do |enabled|
    enabled.virtual? ? enabled.item_ids : fetch_roles
  end
end
enabled_roles_card() click to toggle source
# File set/all/account.rb, line 84
def enabled_roles_card
  fetch :enabled_roles, eager_cache: true, new: { type_id: SessionID }
end
error_on(field, error_key) click to toggle source
# File set/self/signin.rb, line 75
def error_on field, error_key
  errors.add field, t("account_#{error_key}")
end
failed_signin(email) click to toggle source
# File set/self/signin.rb, line 62
def failed_signin email
  errors.add :signin, signin_error_message(account_for(email))
  abort :failure
end
fetch_read_rules() click to toggle source
# File set/all/account.rb, line 37
def fetch_read_rules
  return [] if id == WagnBotID # always_ok, so not needed

  ([AnyoneID] + parties).each_with_object([]) do |party_id, rule_ids|
    next unless (cache = Card::Rule.read_rule_cache[party_id])

    rule_ids.concat cache
  end
end
fetch_roles() click to toggle source
# File set/all/account.rb, line 88
def fetch_roles
  [AnyoneSignedInID] + role_ids_from_roles_trait
end
forbidden_roles() click to toggle source
# File set/right/roles.rb, line 9
def forbidden_roles
  # restore old roles for permission check
  with_old_role_permissions do |new_roles|
    new_roles.reject do |card|
      Card.fetch(card, "*members").ok? :update
    end
  end
end
generate() click to toggle source
# File set/right/salt.rb, line 3
def generate
  self.content = Digest::SHA1.hexdigest "--#{Time.zone.now}--"
end
history?() click to toggle source
# File set/right/password.rb, line 3
def history?
  false
end
input_type() click to toggle source
# File set/right/status.rb, line 4
def input_type
  :radio
end
invitation?() click to toggle source
# File set/type/signup/views.rb, line 2
def invitation?
  Auth.signed_in? && card.can_approve?
end
invite_button() click to toggle source
# File set/type/signup/views.rb, line 21
def invite_button
  return unless invitation?

  button_tag "Send Invitation", situation: "primary"
end
invited?() click to toggle source
# File set/type/signup/views.rb, line 38
def invited?
  !self_signup?
end
mail(context, fields) click to toggle source
Calls superclass method
# File set/right/account/views.rb, line 62
def mail context, fields
  super context, fields.reverse_merge(to: card.email)
end
method_missing(method, *args) click to toggle source
Calls superclass method
# File set/right/account.rb, line 44
def method_missing method, *args
  return super unless args.empty? && (matches = method.match(/^(?<status>.*)\?$/))

  status == matches[:status]
end
new_token(extra_payload) click to toggle source
# File set/right/account/views.rb, line 36
def new_token extra_payload
  Auth::Token.encode card.accounted_id, extra_payload
end
ok_to_read() click to toggle source
Calls superclass method
# File set/right/account.rb, line 17
def ok_to_read
  own_account? || super
end
ok_to_update() click to toggle source

allow account owner to update account field content

Calls superclass method
# File set/abstract/account_field.rb, line 8
def ok_to_update
  return true if own_account? && !name_changed? && !type_id_changed?

  super
end
option_names() click to toggle source
# File set/right/status.rb, line 8
def option_names
  %w[unapproved unverified active blocked system]
end
own_account?() click to toggle source
# File set/all/account.rb, line 23
def own_account?
  # card is +*account card of signed_in user.
  name.part_names[0].key == Auth.as_card.key &&
    name.part_names[1].key == Card[:account].key
end
own_email?() click to toggle source
# File set/right/email.rb, line 44
def own_email?
  name.part_names[0].key == Auth.as_card.key
end
parties() click to toggle source
# File set/all/account.rb, line 11
def parties
  @parties ||= (all_enabled_roles << id).flatten.reject(&:blank?)
end
password_redirect?() click to toggle source
# File set/right/account/events.rb, line 73
def password_redirect?
  Auth.current_id == accounted_id && password.blank?
end
permission_rule_id(action) click to toggle source

force inherit permission on create (cannot be done with rule, because sets are not addressable)

Calls superclass method
# File set/abstract/account_field.rb, line 16
def permission_rule_id action
  if action == :create
    left_permission_rule_id action
  else
    super
  end
end
read_rules() click to toggle source
# File set/all/account.rb, line 29
def read_rules
  @read_rules ||= fetch_read_rules
end
read_rules_hash() click to toggle source
# File set/all/account.rb, line 33
def read_rules_hash
  @read_rules_hash ||= read_rules.each_with_object({}) { |id, h| h[id] = true }
end
recaptcha_on?() click to toggle source
# File set/self/signin.rb, line 46
def recaptcha_on?
  false
end
request_verification() click to toggle source
# File set/type/signup.rb, line 42
def request_verification
  acct = account_subfield
  acct.add_subfield :status, content: "unverified"
  acct.trigger_event! :send_verification_email
end
requiring_token() { |token| ... } click to toggle source
# File set/right/account/events.rb, line 65
def requiring_token
  if !(token = Env.params[:token])
    errors.add :token, "is required"
  else
    yield token
  end
end
reset_password_fail(account) click to toggle source
# File set/self/signin.rb, line 101
def reset_password_fail account
  if account
    error_on :account, :error_not_active
  else
    error_on :email, :error_not_recognized
  end
end
reset_password_failure(error_message) click to toggle source
# File set/right/account/events.rb, line 96
def reset_password_failure error_message
  Auth.as_bot { send_password_reset_email }
  errors.add :content, t(:account_sorry_email_reset, error_msg: error_message)
end
reset_password_success() click to toggle source
# File set/right/account/events.rb, line 90
def reset_password_success
  Auth.signin accounted_id
  success << { id: name, view: :edit }
  abort :success
end
reset_password_voo() click to toggle source
# File set/self/signin.rb, line 170
def reset_password_voo
  voo.title ||= t :account_forgot_password
  voo.edit_structure = [signin_field(:email)]
  voo.hide :help
end
respond_to_missing?(method, _include_private=false) click to toggle source
Calls superclass method
# File set/right/account.rb, line 50
def respond_to_missing? method, _include_private=false
  method.match?(/\?$/) ? true : super
end
role_checkbox() click to toggle source
# File set/type/role.rb, line 10
def role_checkbox
  name = card.disabled? ? "add_item" : "drop_item"
  subformat(Auth.current.field(:disabled_roles, new: {})).card_form :update do
    [check_box_tag(name, card.id, !card.disabled?, class: "_edit-item"),
     render_link]
  end
end
role_ids_from_roles_trait() click to toggle source
# File set/all/account.rb, line 92
def role_ids_from_roles_trait
  Auth.as_bot do
    role_trait = fetch :roles
    role_trait ? role_trait.item_ids : []
  end
end
roles_for_first_user() click to toggle source
# File set/type/user.rb, line 61
def roles_for_first_user
  %i[help_desk shark administrator].map(&:cardname)
end
self_signup?() click to toggle source
# File set/type/signup/views.rb, line 42
def self_signup?
  card.creator_id == AnonymousID
end
send_account_email(email_template) click to toggle source
# File set/right/account.rb, line 35
def send_account_email email_template
  ecard = Card[email_template]
  unless ecard&.type_id == EmailTemplateID
    raise Card::Error, "invalid email template: #{email_template}"
  end

  ecard.deliver self, to: email
end
send_reset_password_email(account) click to toggle source
# File set/self/signin.rb, line 97
def send_reset_password_email account
  Auth.as_bot { account.send_password_reset_email }
end
send_reset_password_email_or_fail() click to toggle source
# File set/self/signin.rb, line 83
def send_reset_password_email_or_fail
  if (account = account_for email_from_subfield)&.active?
    send_reset_password_email account
  else
    reset_password_fail account
  end
end
setup?() click to toggle source
# File set/type/user.rb, line 44
def setup?
  Card::Env.params[:setup]
end
setup_button() click to toggle source
# File set/type/user.rb, line 31
def setup_button
  submit_button text: "Set up", disable_with: "Setting up"
end
setup_form() click to toggle source
# File set/type/user.rb, line 16
def setup_form
  frame_and_form :create do
    [
      setup_hidden_fields,
      _render_name_formgroup,
      account_formgroups,
      setup_form_buttons
    ]
  end
end
setup_form_buttons() click to toggle source
# File set/type/user.rb, line 27
def setup_form_buttons
  button_formgroup { setup_button }
end
setup_hidden_fields() click to toggle source
# File set/type/user.rb, line 35
def setup_hidden_fields
  hidden_tags(
    setup: true,
    success: { redirect: true, mark: path(mark: "") },
    "card[type_id]" => Card.default_accounted_type_id
  )
end
show_account_tab?() click to toggle source
# File set/abstract/accountable.rb, line 25
def show_account_tab?
  card.account.real?
end
signin_button() click to toggle source
# File set/self/signin.rb, line 185
def signin_button
  button_tag t(:account_sign_in), situation: "primary"
end
signin_error_key(account) click to toggle source
# File set/self/signin.rb, line 111
def signin_error_key account
  case
  when account.nil?     then :error_unknown_email
  when !account.active? then :error_not_active
  else                       :error_wrong_password
  end
end
signin_error_message(account) click to toggle source
# File set/self/signin.rb, line 71
def signin_error_message account
  t "account_#{signin_error_key account}"
end
signin_field(name) click to toggle source
# File set/self/signin.rb, line 208
def signin_field name
  nest_name = "".to_name.trait(name)
  [nest_name, { title: name.to_s, view: "titled",
                nest_name: nest_name, skip_perms: true }]
end
signin_success() click to toggle source
# File set/self/signin.rb, line 181
def signin_success
  { redirect: true, mark: (Env.interrupted_action || "*previous") }
end
signup_line() click to toggle source
# File set/type/signup/views.rb, line 32
def signup_line
  ["<strong>#{safe_name}</strong>",
   ("was" if invited?),
   "signed up on #{format_date card.created_at}"].compact.join " "
end
token_expiry_sentence() click to toggle source
# File set/right/account/views.rb, line 32
def token_expiry_sentence
  "Link will expire in #{render_token_days} days"
end
token_url(trigger, extra_payload={}) click to toggle source
# File set/right/account/views.rb, line 26
def token_url trigger, extra_payload={}
  card_url path(action: :update,
                card: { trigger: trigger },
                token: new_token(extra_payload))
end
verification_lines() click to toggle source
# File set/type/signup/views.rb, line 54
def verification_lines
  [verification_sent_line, verification_link_line].compact
end
verification_sent_line() click to toggle source
# File set/type/signup/views.rb, line 58
def verification_sent_line
  account = card.account
  return unless account.email_card.ok?(:read)

  "A verification email has been sent to #{account.email}"
end
verify_and_activate_failure(error_message) click to toggle source
# File set/right/account/events.rb, line 84
def verify_and_activate_failure error_message
  send_verification_email
  errors.add :content,
             "Sorry, #{error_message}. Please check your email for a new activation link."
end
verify_and_activate_success() click to toggle source
# File set/right/account/events.rb, line 77
def verify_and_activate_success
  Auth.signin accounted_id
  Auth.as_bot # use admin permissions for rest of action
  activate_account
  success << ""
end
verifying_token(success, failure) click to toggle source
# File set/right/account/events.rb, line 54
def verifying_token success, failure
  requiring_token do |token|
    result = Auth::Token.decode token
    if result.is_a?(String)
      send failure, result
    else
      send success
    end
  end
end
with_clear_roles() { || ... } click to toggle source
# File set/all/account.rb, line 51
def with_clear_roles
  a = @parties
  b = @all_roles
  c = @all_active_roles
  d = @read_rules
  yield
ensure
  @parties = a
  @all_roles = b
  @all_active_roles = c
  @read_rules = d
end
with_enabled_roles() { |enabled_roles_card| ... } click to toggle source
# File set/all/account.rb, line 78
def with_enabled_roles
  Auth.as_bot do
    Card::Codename.exists?(:enabled_roles) ? yield(enabled_roles_card) : fetch_roles
  end
end
with_old_role_permissions() { |new_roles| ... } click to toggle source
# File set/right/roles.rb, line 18
def with_old_role_permissions
  new_roles = item_cards
  new_content = content
  left.clear_roles
  Auth.update_always_cache Card::Auth.as_id, nil
  self.content = db_content_before_act
  yield new_roles
ensure
  self.content = new_content
end