--[[ $Id: //depot/main/p4-test/server/extensions/examples/login_motd/main.lua#5 $ $DateTime: 2019/06/13 09:17:32 $ $Author: npoole $ $Change: 1816253 $ ]]-- -- 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
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#2 | 30809 | C. Thomas Tyler |
Code drop with newer versions of Extensions. Thanks to @jason_gibon. #review-30810 @jabson_gibson |
||
#1 | 30808 | C. Thomas Tyler |
Promoted from 2019.1/... to main/... |
||
//guest/perforce_software/extensions/2019.1/login_motd/main.lua | |||||
#1 | 25601 | Jason Gibson |
Import example Extensions. p19.1@1797741 |