| 
									
										
										
										
											2020-10-10 14:16:11 +02:00
										 |  |  | # typed: true | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-01 12:29:21 +02:00
										 |  |  | require "delegate" | 
					
						
							|  |  |  | require "etc" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require "system_command" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-09 03:00:13 +02:00
										 |  |  | # A system user. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # @api private | 
					
						
							| 
									
										
										
										
											2020-09-20 05:50:33 +02:00
										 |  |  | class User < SimpleDelegator | 
					
						
							| 
									
										
										
										
											2020-10-10 17:53:31 +02:00
										 |  |  |   include SystemCommand::Mixin | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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) } | 
					
						
							| 
									
										
										
										
											2018-10-01 12:29:21 +02:00
										 |  |  |   def gui? | 
					
						
							|  |  |  |     out, _, status = system_command "who" | 
					
						
							|  |  |  |     return false unless status.success? | 
					
						
							| 
									
										
										
										
											2019-02-19 13:12:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-01 12:29:21 +02:00
										 |  |  |     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)) } | 
					
						
							| 
									
										
										
										
											2018-10-01 12:29:21 +02:00
										 |  |  |   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) | 
					
						
							| 
									
										
										
										
											2018-10-01 12:29:21 +02:00
										 |  |  |   end | 
					
						
							|  |  |  | end |