Skip to content

Commit

Permalink
added: option to join groups based on email domain (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeabakker committed Mar 10, 2014
1 parent 9ea9a71 commit 1ec3b58
Show file tree
Hide file tree
Showing 9 changed files with 257 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ x.x:
- added: plugin setting to indicate if a group is hidden
- added: option to configure a group welcome message (#41)
- added: listing of the email invitations for groups (#42)
- added: option to join groups based on email domain (#43)
- fixed: layout issue in plugin settings
- fixed: you can no join groups you were invited for on the group profile page
- fixed: misleading translation for a group cleanup setting (#22)
Expand Down
38 changes: 38 additions & 0 deletions actions/domain_based.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* Save the domains for domain based joining of a group
*/

$group_guid = (int) get_input("group_guid");
$domains = get_input("domains");

$forward_url = REFERER;

if (!empty($group_guid)) {
$group = get_entity($group_guid);

if (!empty($group) && elgg_instanceof($group, "group")) {
if ($group->canEdit()) {

if (!empty($domains)) {
$domains = string_to_tag_array($domains);
$domains = "|" . implode("|", $domains) . "|";

$group->setPrivateSetting("domain_based", $domains);
} else {
$group->removePrivateSetting("domain_based");
}

system_message(elgg_echo("group_tools:action:domain_based:success"));
$forward_url = $group->getURL();
} else {
register_error(elgg_echo("groups:cantedit"));
}
} else {
register_error(elgg_echo("groups:notfound:details"));
}
} else {
register_error(elgg_echo("InvalidParameterException:MissingParameter"));
}

forward($forward_url);
11 changes: 11 additions & 0 deletions languages/en.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
'group_tools:settings:invite_members:default_off' => "Yes, default off",
'group_tools:settings:invite_members:default_on' => "Yes, default on",
'group_tools:settings:invite_members:description' => "Group owners/admins can enable/disable this for their group",
'group_tools:settings:domain_based' => "Enable domain based groups",
'group_tools:settings:domain_based:description' => "Users can join a group based on their e-mail domain. During registration they will auto join groups based on their e-mail domain.",

'group_tools:settings:mail' => "Allow group mail (allows group admins to send a message to all members)",

Expand Down Expand Up @@ -412,6 +414,15 @@
// email invitations
'group_tools:action:revoke_email_invitation:error' => "An error occured while revoking the invitation, please try again",
'group_tools:action:revoke_email_invitation:success' => "The invitation was revoked",

// domain based groups
'group_tools:join:domain_based:tooltip' => "Because of a matching e-mail domain, you can join this group.",

'group_tools:domain_based:title' => "Configure e-mail domains",
'group_tools:domain_based:description' => "When you configure one (or more) e-mail domains, users with that e-mail domain will automaticly join your group upon registration. Also if you have a closed group user with a matching e-mail domain can join without requesting membership. You can configure multipe domains by using a comma. Don't include the @ sign",

'group_tools:action:domain_based:success' => "The new e-mail domains were saved",

);

add_translation("en", $english);
9 changes: 9 additions & 0 deletions lib/events.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,15 @@ function group_tools_join_site_handler($event, $type, $relationship) {
}
}

// find domain based groups
$groups = group_tools_get_domain_based_groups($user, $site_guid);
if (!empty($groups)) {
foreach ($groups as $group) {
// join the group
$group->join($user);
}
}

// restore access settings
elgg_set_ignore_access($ia);
}
Expand Down
94 changes: 94 additions & 0 deletions lib/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -707,3 +707,97 @@ function group_tools_show_hidden_indicator(ElggGroup $group) {

return $result;
}

/**
* Check the plugin setting which enables domain based groups
*
* @return boolean
*/
function group_tools_domain_based_groups_enabled() {
static $result;

if (!isset($result)) {
$result = false;

$setting = elgg_get_plugin_setting("domain_based", "group_tools");
if ($setting == "yes") {
$result = true;
}
}

return $result;
}

/**
* Check if the domain based settings for this group match the user
*
* @param ElggGroup $group the group to match to
* @param ElggUser $user the user to check (defaults to current user)
*
* @return boolean true if the domain of the user is found in the group settings
*/
function group_tools_check_domain_based_group(ElggGroup $group, ElggUser $user = null) {
$result = false;

if (group_tools_domain_based_groups_enabled()) {
if (empty($user)) {
$user = elgg_get_logged_in_user_entity();
}

if (!empty($group) && elgg_instanceof($group, "group") && !empty($user) && elgg_instanceof($user, "user")) {
$domains = $group->getPrivateSetting("domain_based");

if (!empty($domains)) {
$domains = explode("|", trim($domains, "|"));

list(,$domain) = explode("@", $user->email);

if (in_array($domain, $domains)) {
$result = true;
}
}
}
}

return $result;
}

/**
* Get all groups based on the email domain of the user from the group settings
*
* @param ElggUser $user The user used to base the search
* @param int $site_guid (optional) the site guid to limit the search to, defaults to current site
*
* @return bool|ElggGroup[] false or an array of found groups
*/
function group_tools_get_domain_based_groups(ElggUser $user, $site_guid = 0) {
$result = false;

if (group_tools_domain_based_groups_enabled()) {
if (empty($site_guid)) {
$site_guid = elgg_get_site_entity()->getGUID();
}

if (!empty($user) && elgg_instanceof($user, "user")) {
list(, $domain) = explode("@", $user->email);

$options = array(
"type" => "group",
"limit" => false,
"site_guids" => $site_guid,
"private_setting_name_value_pairs" => array(
"name" => "domain_based",
"value" => "%|" . $domain . "|%",
"operand" => "LIKE"
)
);
$groups = elgg_get_entities_from_private_settings($options);
if (!empty($groups)) {
$result = $groups;
}
}
}

return $result;
}

55 changes: 55 additions & 0 deletions lib/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ function group_tools_menu_title_handler($hook, $type, $return_value, $params) {
$menu_item->setText(elgg_echo("groups:join"));
$menu_item->setTooltip(elgg_echo("group_tools:join:already:tooltip"));
$menu_item->setHref(elgg_add_action_tokens_to_url(elgg_get_site_url() . "action/groups/join?user_guid=" . $user->getGUID() . "&group_guid=" . $page_owner->getGUID()));
} elseif (group_tools_check_domain_based_group($page_owner, $user)) {
// user has a matching email domain
$menu_item->setName("groups:join");
$menu_item->setText(elgg_echo("groups:join"));
$menu_item->setTooltip(elgg_echo("group_tools:join:domain_based:tooltip"));
$menu_item->setHref(elgg_add_action_tokens_to_url(elgg_get_site_url() . "action/groups/join?user_guid=" . $user->getGUID() . "&group_guid=" . $page_owner->getGUID()));
}

break;
Expand Down Expand Up @@ -536,3 +542,52 @@ function group_tools_admin_transfer_permissions_hook($hook, $type, $return_value

return $result;
}

/**
* A prepend hook to the groups/join action
*
* @param string $hook 'action'
* @param string $type 'groups/join'
* @param bool $return_value true, return false to stop the action
* @param null $params passed on params
*
* @return bool
*/
function group_tools_join_group_action_handler($hook, $type, $return_value, $params) {
// hacky way around a short comming of Elgg core to allow users to join a group
if (group_tools_domain_based_groups_enabled()) {
elgg_register_plugin_hook_handler("permissions_check", "group", "group_tools_permissions_check_groups_join_hook");
}
}

/**
* A hook on the ->canEdit() of a group. This is done to allow e-mail domain users to join a group
*
* Note: this is a very hacky way arround a short comming of Elgg core
*
* @param string $hook 'permissions_check'
* @param string $type 'group'
* @param bool $return_value is the current user allowed to edit the group
* @param mixed $params passed on params
*
* @return bool
*/
function group_tools_permissions_check_groups_join_hook($hook, $type, $return_value, $params) {
$result = $return_value;

if (!$result && group_tools_domain_based_groups_enabled()) {
// domain based groups are enabled, lets check if this user is allowed to join based on that
if (!empty($params) && is_array($params)) {
$group = elgg_extract("entity", $params);
$user = elgg_extract("user", $params);

if (!empty($group) && elgg_instanceof($group, "group") && !empty($user) && elgg_instanceof($user ,"user")) {
if (group_tools_check_domain_based_group($group, $user)) {
$result = true;
}
}
}
}

return $result;
}
5 changes: 5 additions & 0 deletions start.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ function group_tools_init() {
// configure a group welcome message
elgg_extend_view("groups/edit", "group_tools/forms/welcome_message");

// configure domain based group join
elgg_extend_view("groups/edit", "group_tools/forms/domain_based");

// show group status in owner block
elgg_extend_view("page/elements/owner_block/extend", "group_tools/owner_block");
// show group status in stats (on group profile)
Expand All @@ -121,6 +124,7 @@ function group_tools_init() {
elgg_register_plugin_hook_handler("widget_url", "widget_manager", "group_tools_widget_url_handler");
elgg_register_plugin_hook_handler("access:default", "user", "group_tools_access_default_handler");
elgg_register_plugin_hook_handler("access:collections:write", "user", "group_tools_access_write_handler");
elgg_register_plugin_hook_handler("action", "groups/join", "group_tools_join_group_action_handler");

// actions
elgg_register_action("group_tools/admin_transfer", dirname(__FILE__) . "/actions/admin_transfer.php");
Expand All @@ -131,6 +135,7 @@ function group_tools_init() {
elgg_register_action("group_tools/default_access", dirname(__FILE__) . "/actions/default_access.php");
elgg_register_action("group_tools/invite_members", dirname(__FILE__) . "/actions/invite_members.php");
elgg_register_action("group_tools/welcome_message", dirname(__FILE__) . "/actions/welcome_message.php");
elgg_register_action("group_tools/domain_based", dirname(__FILE__) . "/actions/domain_based.php");

elgg_register_action("group_tools/toggle_special_state", dirname(__FILE__) . "/actions/admin/toggle_special_state.php", "admin");
elgg_register_action("group_tools/fix_auto_join", dirname(__FILE__) . "/actions/admin/fix_auto_join.php", "admin");
Expand Down
26 changes: 26 additions & 0 deletions views/default/group_tools/forms/domain_based.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

$group = elgg_extract("entity", $vars);
if (!empty($group) && elgg_instanceof($group, "group") && group_tools_domain_based_groups_enabled()) {
$title = elgg_echo("group_tools:domain_based:title");

$form_body = "<div class='mbm'>" . elgg_echo("group_tools:domain_based:description") . "</div>";

$domains = $group->getPrivateSetting("domain_based");
if (!empty($domains)) {
$domains = explode("|", trim($domains, "|"));
}

$form_body .= "<div>";
$form_body .= elgg_view("input/tags", array("name" => "domains", "value" => $domains));
$form_body .= "</div>";

$form_body .= "<div class='elgg-foot mtm'>";
$form_body .= elgg_view("input/hidden", array("name" => "group_guid", "value" => $group->getGUID()));
$form_body .= elgg_view("input/submit", array("value" => elgg_echo("save")));
$form_body .= "</div>";

$content = elgg_view("input/form", array("action" => "action/group_tools/domain_based", "body" => $form_body));

echo elgg_view_module("info", $title, $content);
}
Loading

0 comments on commit 1ec3b58

Please sign in to comment.