Browse Source

Add task to change a user's role

master
Les De Ridder 4 months ago
parent
commit
d89ab1bc5f
3 changed files with 77 additions and 1 deletions
  1. +3
    -0
      src/operations/change_user_role.cr
  2. +2
    -1
      src/pages/me/show_page.cr
  3. +72
    -0
      tasks/change_role.cr

+ 3
- 0
src/operations/change_user_role.cr View File

@@ -0,0 +1,3 @@
class ChangeUserRole < User::SaveOperation
# TODO: Log change
end

+ 2
- 1
src/pages/me/show_page.cr View File

@@ -1,7 +1,8 @@
class Me::ShowPage < MainLayout
def content
h1 "This is your profile"
h3 "Email: #{@current_user.email}"
h3 "Email: #{@current_user.email}"
h3 "Role: #{@current_user.primary_role!.description}"
helpful_tips
end



+ 72
- 0
tasks/change_role.cr View File

@@ -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…
Cancel
Save