Add task to change a user's role
This commit is contained in:
parent
bf415d7a2e
commit
d89ab1bc5f
|
@ -0,0 +1,3 @@
|
||||||
|
class ChangeUserRole < User::SaveOperation
|
||||||
|
# TODO: Log change
|
||||||
|
end
|
|
@ -1,7 +1,8 @@
|
||||||
class Me::ShowPage < MainLayout
|
class Me::ShowPage < MainLayout
|
||||||
def content
|
def content
|
||||||
h1 "This is your profile"
|
h1 "This is your profile"
|
||||||
h3 "Email: #{@current_user.email}"
|
h3 "Email: #{@current_user.email}"
|
||||||
|
h3 "Role: #{@current_user.primary_role!.description}"
|
||||||
helpful_tips
|
helpful_tips
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,72 @@
|
||||||
|
class App::ChangeRole < LuckyCli::Task
|
||||||
|
def initialize(@quiet : Bool = false)
|
||||||
|
end
|
||||||
|
|
||||||
|
summary "Change a user's role"
|
||||||
|
|
||||||
|
getter io : IO = STDOUT
|
||||||
|
|
||||||
|
def call(@io : IO = STDOUT)
|
||||||
|
if username_is_present
|
||||||
|
user = get_user
|
||||||
|
|
||||||
|
if user.nil?
|
||||||
|
io.puts @error.colorize(:red)
|
||||||
|
else
|
||||||
|
role = ask_role
|
||||||
|
|
||||||
|
if role.nil?
|
||||||
|
io.puts @error.colorize(:red)
|
||||||
|
else
|
||||||
|
ChangeUserRole.update!(user, primary_role_id: role.id)
|
||||||
|
|
||||||
|
io.puts "#{user.name.colorize(:green)}'s role set to #{role.name.colorize(:green)} (#{role.description.colorize(:green)})"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
io.puts @error.colorize(:red)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def help_message
|
||||||
|
<<-TEXT
|
||||||
|
#{summary}
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
lucky app.change_role username
|
||||||
|
TEXT
|
||||||
|
end
|
||||||
|
|
||||||
|
private def ask_role
|
||||||
|
roles = RoleQuery.new
|
||||||
|
|
||||||
|
io.puts "Choose a role:\n".colorize.bold
|
||||||
|
|
||||||
|
roles.each do |role|
|
||||||
|
io.puts "[#{role.id}] #{role.name} (#{role.description})"
|
||||||
|
end
|
||||||
|
|
||||||
|
io.print "\nRole number: ".colorize.bold
|
||||||
|
input = io.gets
|
||||||
|
|
||||||
|
if input.nil?
|
||||||
|
@error = "No role number entered"
|
||||||
|
nil
|
||||||
|
else
|
||||||
|
role_id = input.to_i { -1 }
|
||||||
|
@error = "No role with number #{input}"
|
||||||
|
roles.id(role_id).first?
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private def get_user
|
||||||
|
@error = "User '#{ARGV.first}' not found"
|
||||||
|
user = UserQuery.new.name(ARGV.first).first?
|
||||||
|
end
|
||||||
|
|
||||||
|
private def username_is_present
|
||||||
|
@error = "Username is required. Example: lucky app.change_role john"
|
||||||
|
ARGV.first?
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue