--[[ $Id: //depot/p19.1/p4-test/server/extensions/examples/login_motd/main.lua#6 $ $DateTime: 2019/04/30 13:18:49 $ $Author: jgibson $ $Change: 1796591 $ ]]-- -- Add the Extension's archive directory to the package search path -- so we can require() our assets. package.path = Helix.Core.Server.GetArchDirFileName( "?.lua" ) local utils = require "ExtUtils" -- Keep some state about the initialization of the Extension so we -- don't keep retrieving static data all the time. local initDone = false function init() if not initdone then initDone = 1 utils.init() end end -- This Extension doesn't have any global configuration. function GlobalConfigFields() return {} end -- Each instance of this Extension has three things it can be configured -- with. The table we return ends up in the form accessed via -- 'p4 extension --configure extName --name extInstance'. function InstanceConfigFields() return { -- This gets sent to the user prior to authentication. message = "The message displayed before login.", groups = [[Space-separated list of groups whose members receive the message. An empty list means the message goes to everyone.]], serverID = "Server ID to limit the message to." } end -- Here, the Extension tells the server which events it needs to be -- registered for. The keys of the table are the same as the set -- of available triggers. function InstanceConfigEvents() return { command = "pre-user-login" } end -- Connect to the hosting server and see if the user running -- the login is a member of a particular set of groups. function isUserInGroups( user, groups ) local p4 = P4.P4:new() p4.prog = utils.getID() -- This function automatically logs the P4USER specified in the -- Extension's global config into the server running the Extension. p4:autoconnect() p4:connect() local grData = p4:run( "groups", "-u", "-i", user ) local gs = {} for i, dict in ipairs( grData ) do gs[ dict[ "group" ] ] = 1 end for k, v in pairs( groups ) do if gs[ k ] ~= nil then return true end end return false end -- The Extension callbacks for events in the server mirror the names -- of the keys in the InstanceConfigEvents() function above. In this case -- this is a 'command' event that gets run before a command begins -- execution. function Command() init() -- Get the instance configuration data needed for the Extension to -- behave the way it is configured. local groups = utils.iCfgData[ "groups" ] local ngroups = utils.iCfgData[ "ngroups" ] local serverID = utils.iCfgData[ "serverID" ] local message = utils.iCfgData[ "message" ] if serverID ~= nil and Helix.Core.Server.GetVar( "serverid" ) ~= serverID then return true end -- Don't double-display the message - once from the replica and once from the master. if serverID == nil and Helix.Core.Server.GetVar( "serverservices" ) ~= "commit-server" then return true end local err, ret if ngroups > 0 then err, ret = pcall( function() return isUserInGroups( Helix.Core.Server.GetVar( "user" ), groups ) end ) end if err == false then Helix.Core.Server.SetClientMsg( utils.msgHeader() .. ret ) return false end if ret == true or ngroups == 0 then Helix.Core.Server.SetClientMsg( utils.msgHeader() .. "\n\n" .. message ) end return true end