Class: Cyclid::Cli::Member

Inherits:
Thor
  • Object
show all
Defined in:
lib/cyclid/cli/organization/member.rb

Overview

Commands for managing organization members

Instance Method Summary collapse

Instance Method Details

#add(*users) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/cyclid/cli/organization/member.rb', line 22

def add(*users)
  org = client.org_get(client.config.organization)

  # Concat the new list with the existing list and remove any
  # duplicates.
  user_list = org['users']
  user_list.concat users
  user_list.uniq!

  client.org_modify(client.config.organization,
                    members: user_list)
rescue StandardError => ex
  abort "Failed to add users to the organization: #{ex}"
end

#listObject



72
73
74
75
76
77
78
79
# File 'lib/cyclid/cli/organization/member.rb', line 72

def list
  org = client.org_get(client.config.organization)
  org['users'].each do |user|
    puts user
  end
rescue StandardError => ex
  abort "Failed to get organization members: #{ex}"
end

#permission(user, permission) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/cyclid/cli/organization/member.rb', line 51

def permission(user, permission)
  perms = case permission.downcase
          when 'admin'
            { 'admin' => true, 'write' => true, 'read' => true }
          when 'write'
            { 'admin' => false, 'write' => true, 'read' => true }
          when 'read'
            { 'admin' => false, 'write' => false, 'read' => true }
          when 'none'
            { 'admin' => false, 'write' => false, 'read' => false }
          else
            raise "invalid permission #{permission}"
          end

  client.org_user_permissions(client.config.organization, user, perms)
rescue StandardError => ex
  abort "Failed to modify user permissions: #{ex}"
end

#remove(*users) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/cyclid/cli/organization/member.rb', line 103

def remove(*users)
  org = client.org_get(client.config.organization)

  # Remove any users that exist as members of this organization;
  # ask for confirmation on a per-user basis (unless '-f' was passed)
  user_list = org['users']
  user_list.delete_if do |user|
    if users.include? user
      if options[:force]
        true
      else
        Formatter.ask "Remove user #{user}: are you sure? (Y/n)"
        STDIN.getc.chr.casecmp('y').zero?
      end
    end
  end
  user_list.uniq!

  client.org_modify(client.config.organization,
                    members: user_list)
rescue StandardError => ex
  abort "Failed to remove users from the organization: #{ex}"
end

#show(user) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/cyclid/cli/organization/member.rb', line 82

def show(user)
  user = client.org_user_get(client.config.organization, user)

  # Pretty print the user details
  Formatter.colorize 'Username', user['username']
  Formatter.colorize 'Email: ', user['email']
  Formatter.colorize 'Permissions'
  user['permissions'].each do |k, v|
    Formatter.colorize "\t#{k.capitalize}", v.to_s
  end
rescue StandardError => ex
  abort "Failed to get user: #{ex}"
end