Notifications on user login

on 08-Oct-2009 | Comments ( 1 ) Tags: Evolution

There are times when you might want to know when a certain user logs into the system. The following code will send email notifications when a Web User or a Manager User access the system.

The plugin will make use of the OnWebLogin and OnManagerLogin events.

The plugin

Create a new plugin and paste the following code:

$e = &$modx->Event;
global $modx;

if($email && $wun && $e->name == "OnWebLogin"){//check Web User Names
	$aWun = explode(',',$wun);
	foreach($aWun as $un){//loop usernames
		if($username == trim($un)){
			$subject = 'Web User Login';
			$message = $username . ' has logged in on ' . date('l jS \of F Y h:i:s A');
			sendEmail($email, $subject, $message);
		}
	}
}

if($email && $mun && $e->name == "OnManagerLogin"){//check Manager User Names
	$aMun = explode(',',$mun);
	foreach($aMun as $un){//loop username
		if($username == trim($un)){
			$subject = 'Manager User Login';
			$message = $username . ' has logged in on ' . date('l jS \of F Y h:i:s A');
			sendEmail($email, $subject, $message);
		}
	}
}

// Send email
function sendEmail($to, $subject, $message) {
	$headers ="MIME-Version: 1.0\n".
              "Content-type: text/html; charset=iso-8859-1\n".
              "From: ".$_SERVER['SERVER_NAME']."<no-reply@{$_SERVER['HTTP_HOST']}>\n".
              "X-Mailer: PHP/".phpversion();

$body = <<<EOD
$message
EOD;

   $mail = mail($to, $subject, $body, $headers);
}

Name the plugin accordingly. Eg:Notifications on Login

Configuration

Go to the Configuration Tab and paste the following in the Plugin configuration box:

&wun=Web User Names;text;&mun=Manager User Names;text;&email=Email to:;text;

Click Update Parameter Display and enter your configuration for the plugin. You can track multiple users by entering a comma separated list of usernames.

One more step

Go to System Events and check OnWebLogin and OnManagerLogin events. Save the plugin.

 

Any other events you might want to track? Leave a message.

Write a comment

  • Required fields are marked with *.

If you have trouble reading the code, click on the code itself to generate a new random code.
 
jordi
Posts: 1
Comment
Thanks!
Reply #1 on : Thu May 05, 2011, 02:26:33
Thanks for this tutorial. It's very useful :)

Quick modx Evolution Tags

  • cached [[snippet]] or uncached [!snippet!]
  • {{chunk}}
  • [+placeholder+]
  • [*resourceField/TV*]
  • [^timing^]
  • [~link~]

Quick modx Revolution Tags

  • [[snippet]]
  • [[$chunk]]
  • [[+placeholder]]
  • [[*resourceField/TV]]
  • [[~link]]
  • [^timing^]
  • [[++systemSetting]]
  • [[%languageStringKey]]
  • all tags can be called un-cached like: [[! snippet]]

Timing Tags (Evo and Revo)

  • [^qt^] - Query time
  • [^q^] - Query count
  • [^p^] - Parse time
  • [^t^] - Total time
  • [^s^] - Source
© modxRULES! 2009-2014