Skip to content

Instantly share code, notes, and snippets.

@twosixcode
Created January 9, 2014 04:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twosixcode/cae6eec0ae3c963e5d07 to your computer and use it in GitHub Desktop.
Save twosixcode/cae6eec0ae3c963e5d07 to your computer and use it in GitHub Desktop.
ExpressionEngine extension to prevent members of a specific member group from deleting members in some other member group. http://expressionengine.stackexchange.com/a/18208/22
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* ExpressionEngine - by EllisLab
*
* @package ExpressionEngine
* @author ExpressionEngine Dev Team
* @copyright Copyright (c) 2003 - 2011, EllisLab, Inc.
* @license http://expressionengine.com/user_guide/license.html
* @link http://expressionengine.com
* @since Version 2.0
* @filesource
*/
// ------------------------------------------------------------------------
/**
* Protected Member Groups Extension
*
* @package ExpressionEngine
* @subpackage Addons
* @category Extension
* @author Example Only
* @link http://example.com
*/
class Protected_member_groups_ext {
public $settings = array();
public $description = 'Prevent members of a specific member group from deleting members in some other member group.';
public $docs_url = '';
public $name = 'Protected Member Groups';
public $settings_exist = 'n';
public $version = '1.0';
private $EE;
/**
* Constructor
*
* @param mixed Settings array or empty string if none exist.
*/
public function __construct($settings = '')
{
$this->EE =& get_instance();
$this->settings = $settings;
}// ----------------------------------------------------------------------
/**
* Activate Extension
*
* This function enters the extension into the exp_extensions table
*
* @see http://codeigniter.com/user_guide/database/index.html for
* more information on the db class.
*
* @return void
*/
public function activate_extension()
{
// Setup custom settings in this array.
$this->settings = array();
$data = array(
'class' => __CLASS__,
'method' => 'member_delete',
'hook' => 'member_delete',
'settings' => serialize($this->settings),
'version' => $this->version,
'enabled' => 'y'
);
$this->EE->db->insert('extensions', $data);
}
// ----------------------------------------------------------------------
/**
* member_delete
*
* @param
* @return
*/
public function member_delete($member_ids)
{
// Array of member group_ids for which to restrict delete permissions
$supervised_member_groups = array('7');
// Array of member group_ids for wich deletion is disallowed
$protected_member_groups = array('5','6');
// The group_id of the current admin
$current_admin_group_id = ee()->session->userdata('group_id');
// Check if the current admin is in the supervised member group
if (in_array($current_admin_group_id, $supervised_member_groups))
{
// Generate an array of group_ids to which the deleted member(s) belong(s)
$member_groups = array();
$query = ee()->db->select('group_id')->where_in('member_id', $member_ids)->get('members');
foreach ($query->result() as $row)
{
array_push($member_groups, $row->group_id);
}
// Check if the deleted member belongs to one of the protected member groups
// and if so throw an error
$overlap = count(array_intersect($protected_member_groups, $member_groups));
if ($overlap) show_error('You are attempting to delete a member from a protected group. Please adjust your selection.');
}
return $member_ids;
}
// ----------------------------------------------------------------------
/**
* Disable Extension
*
* This method removes information from the exp_extensions table
*
* @return void
*/
function disable_extension()
{
$this->EE->db->where('class', __CLASS__);
$this->EE->db->delete('extensions');
}
// ----------------------------------------------------------------------
/**
* Update Extension
*
* This function performs any necessary db updates when the extension
* page is visited
*
* @return mixed void on update / false if none
*/
function update_extension($current = '')
{
if ($current == '' OR $current == $this->version)
{
return FALSE;
}
}
// ----------------------------------------------------------------------
}
/* End of file ext.protected_member_groups.php */
/* Location: /system/expressionengine/third_party/protected_member_groups/ext.protected_member_groups.php */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment