Skip to content

Commit

Permalink
cli: Improve create-users to support roles
Browse files Browse the repository at this point in the history
  • Loading branch information
jhf committed Jan 13, 2025
1 parent 32b73a9 commit 1643c13
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions cli/src/statbus.cr
Original file line number Diff line number Diff line change
Expand Up @@ -193,25 +193,9 @@ class StatBus
@valid_from : String = Time.utc.to_s("%Y-%m-%d")
@valid_to = "infinity"
@user_email : String | Nil = nil
@available_roles : Array(String) = ["super_user", "regular_user", "restricted_user", "external_user"]

private def load_available_roles
Dir.cd(@project_directory) do
config = StatBusConfig.new(@project_directory, @verbose)
DB.connect(config.connection_string) do |db|
@available_roles = db.query_all(
"SELECT unnest(enum_range(NULL::public.statbus_role_type))::text AS role",
as: {role: String}
).map { |r| r[:role] }
end
end
rescue ex
STDERR.puts "Warning: Could not load roles from database: #{ex.message}"
end

def initialize
@project_directory = initialize_project_directory
load_available_roles
begin
option_parser = build_option_parser
option_parser.parse
Expand Down Expand Up @@ -1118,6 +1102,19 @@ class StatBus
end

DB.connect(config.connection_string) do |db|
available_roles : Array(String) = ["super_user", "regular_user", "restricted_user", "external_user"]
begin
Dir.cd(@project_directory) do
available_roles = db.query_all(
"SELECT unnest(enum_range(NULL::public.statbus_role_type))::text AS role",
as: {role: String}
).map { |r| r[:role] }
end
rescue ex
STDERR.puts "Warning: Could not load roles from database: #{ex.message}"
available_roles = ["super_user", "regular_user", "restricted_user", "external_user"]
end

# Read users from YAML file
users_yaml = File.read(".users.yml")
users = YAML.parse(users_yaml)
Expand All @@ -1129,9 +1126,9 @@ class StatBus
role = user["role"]?.try(&.as_s) || "regular_user"

# Validate role
if !@available_roles.includes?(role)
if !available_roles.includes?(role)
STDERR.puts "Error: Invalid role '#{role}' for user #{email}"
STDERR.puts "Available roles: #{@available_roles.join(", ")}"
STDERR.puts "Available roles: #{available_roles.join(", ")}"
exit(1)
end

Expand Down

0 comments on commit 1643c13

Please sign in to comment.