38 lines
732 B
Ruby
Raw Normal View History

2020-10-10 14:16:11 +02:00
# typed: true
# frozen_string_literal: true
require "delegate"
require "etc"
require "system_command"
2020-08-09 03:00:13 +02:00
# A system user.
#
# @api private
class User < SimpleDelegator
2020-10-10 15:53:14 +02:00
include Kernel
extend T::Sig
2020-08-09 03:00:13 +02:00
# Return whether the user has an active GUI session.
2020-10-10 15:53:14 +02:00
sig { returns(T::Boolean) }
def gui?
out, _, status = system_command "who"
return false unless status.success?
out.lines
.map(&:split)
.any? { |user, type,| user == self && type == "console" }
end
2020-08-09 03:00:13 +02:00
# Return the current user.
2020-10-10 15:53:14 +02:00
sig { returns(T.nilable(T.attached_class)) }
def self.current
2020-07-09 01:35:39 +05:30
return @current if defined?(@current)
pwuid = Etc.getpwuid(Process.euid)
return if pwuid.nil?
@current = new(pwuid.name)
end
end