class Shelly::CLI::User
Public Instance Methods
add(email = nil)
click to toggle source
# File lib/shelly/cli/user.rb, line 43 def add(email = nil) organization = organization_present?(options[:organization], "user add [EMAIL]") user_email = email || ask_for_email({:guess_email => false}) owner = yes?("Should this user have owner privileges? (yes/no)") organization.send_invitation(user_email, owner) say "Sending invitation to #{user_email} to work on #{organization} organization", :green rescue Client::ForbiddenException say_error "You have to be organization's owner to add new members" rescue Client::NotFoundException => e raise unless e.resource == :organization say_error "Organization '#{options[:organization]}' not found", :with_exit => false say_error "You can list organizations you have access to with `shelly organization list`" rescue Client::ValidationException => e if e.errors.include?(["email", "#{email} has been already taken"]) say_error "User #{email} is already in the organization #{organization}" else e.each_error { |error| say_error error, :with_exit => false } exit 1 end end
delete(email = nil)
click to toggle source
# File lib/shelly/cli/user.rb, line 68 def delete(email = nil) organization = organization_present?(options[:organization], "user delete [EMAIL]") user_email = email || ask_for_email({:guess_email => false}) organization.delete_member(user_email) say "User #{user_email} deleted from organization #{organization}" rescue Client::ForbiddenException say_error "You have to be organization's owner to remove members" rescue Client::ConflictException => e say_error e[:message] rescue Client::NotFoundException => e if e.resource == :user say_error "User '#{user_email}' not found", :with_exit => false say_error "You can list users with `shelly user list`" elsif e.resource == :organization say_error "Organization '#{options[:organization]}' not found", :with_exit => false say_error "You can list organizations you have access to with `shelly organization list`" else raise end end
fetch_organization(name)
click to toggle source
# File lib/shelly/cli/user.rb, line 107 def fetch_organization(name) Shelly::Organization.new("name" => name).tap do |org| org.members end end
human_owner(owner)
click to toggle source
# File lib/shelly/cli/user.rb, line 92 def human_owner(owner) owner ? "owner" : "member" end
list()
click to toggle source
# File lib/shelly/cli/user.rb, line 15 def list organizations = if options[:organization] organization = fetch_organization(options[:organization]) [organization] else Shelly::User.new.organizations end organizations.each do |organization| say organization.name, :green if organization.memberships.present? members_table = organization.owners.map { |owner| [owner["email"], " | owner"] } members_table += organization.members.map { |member| [member["email"], " | member"] } members_table += organization.inactive_members.map { |inactive| [inactive["email"] + " (invited)", " | #{human_owner(inactive["owner"])}"] } print_table(members_table, :ident => 2, :colwidth => 45) say_new_line end end rescue Client::NotFoundException => e raise unless e.resource == :organization say_error "Organization '#{options[:organization]}' not found", :with_exit => false say_error "You can list organizations you have access to with `shelly organization list`" end
organization_present?(name, action)
click to toggle source
# File lib/shelly/cli/user.rb, line 96 def organization_present?(name, action) unless name say_error "You have to specify organization", :with_exit => false say "Select organization using `shelly #{action} --organization ORGANIZATION_NAME`" Shelly::CLI::Organization.new.list exit 1 else fetch_organization(name) end end