Module: Card::Auth::Permissions

Included in:
Card::Auth
Defined in:
card/lib/card/auth/permissions.rb

Overview

singleton permission methods

Instance Method Summary collapse

Instance Method Details

#admin?(user_id) ⇒ true/false

test whether user is an administrator

Parameters:

  • user_id (Integer)

Returns:

  • (true/false)


61
62
63
# File 'card/lib/card/auth/permissions.rb', line 61

def admin? user_id
  has_role? user_id, Card::AdministratorID
end

#always_cacheObject



37
38
39
# File 'card/lib/card/auth/permissions.rb', line 37

def always_cache
  Card.cache.read("ALWAYS") || {}
end

#always_ok?true/false

user has “root” permissions

Returns:

  • (true/false)


7
8
9
10
11
12
13
14
15
# File 'card/lib/card/auth/permissions.rb', line 7

def always_ok?
  usr_id = as_id
  case usr_id
  when Card::WagnBotID then true # cannot disable
  when nil             then false
  else
    always_ok_usr_id? usr_id
  end
end

#always_ok_usr_id?(usr_id, force_cache_update = false) ⇒ true/false

specified user has root permission

Parameters:

  • usr_id (Integer)

Returns:

  • (true/false)


20
21
22
23
24
25
26
27
# File 'card/lib/card/auth/permissions.rb', line 20

def always_ok_usr_id? usr_id, force_cache_update=false
  always = always_cache
  if always[usr_id].nil? || force_cache_update
    update_always_cache usr_id, admin?(usr_id)
  else
    always[usr_id]
  end
end

#createable_typesArray of strings

list of names of cardtype cards that current user has perms to create

Returns:

  • (Array of strings)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'card/lib/card/auth/permissions.rb', line 43

def createable_types
  type_names =
    Auth.as_bot do
      Card.search(
        { type: Card::CardtypeID, return: :name,
          not: { codename: ["in"] + Card.config.non_createable_types } },
        "find createable types"
      )
    end

  type_names.select do |name|
    Card.new(type: name).ok? :create
  end.sort
end

#has_role?(user_id, role_id) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
68
69
# File 'card/lib/card/auth/permissions.rb', line 65

def has_role? user_id, role_id
  return false unless user_id && role_id

  Card[user_id].all_enabled_roles.include? role_id
end

#update_always_cache(usr_id, value) ⇒ Object



29
30
31
32
33
34
35
# File 'card/lib/card/auth/permissions.rb', line 29

def update_always_cache usr_id, value
  always = always_cache
  always = always.dup if always.frozen?
  always[usr_id] = value
  Card.cache.write "ALWAYS", always
  value
end