Skip to content
This repository was archived by the owner on Oct 29, 2020. It is now read-only.

Commit 23ef5fd

Browse files
Merge pull request #3577 from aaronschachter/signup_endpoint
Signup API endpoint
2 parents 92eb6a8 + 02640f0 commit 23ef5fd

File tree

7 files changed

+68
-15
lines changed

7 files changed

+68
-15
lines changed

lib/modules/dosomething/dosomething_api/dosomething_api.info

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ dependencies[] = services
66
features[ctools][] = services:services:3
77
features[features_api][] = api:2
88
features[services_endpoint][] = drupalapi
9-
mtime = 1416603771
9+
mtime = 1417467369

lib/modules/dosomething/dosomething_api/dosomething_api.services.inc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ function dosomething_api_default_services_endpoint() {
6060
),
6161
),
6262
'targeted_actions' => array(
63+
'signup' => array(
64+
'enabled' => '1',
65+
),
6366
'reportback' => array(
6467
'enabled' => '1',
6568
),

lib/modules/dosomething/dosomething_api/resources/campaign_resource.inc

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,13 @@ function _campaign_resource_defintion() {
3838
'type' => 'int',
3939
'description' => 'The nid of the Campaign node to signup for.',
4040
),
41-
// @todo: We'll want this for SMS campaigns where uid doesn't exist?
42-
// array(
43-
// 'name' => 'uid',
44-
// 'optional' => TRUE,
45-
// 'source' => array('data' => 'uid'),
46-
// 'description' => 'The User uid to signup',
47-
// 'type' => 'int',
48-
// ),
41+
array(
42+
'name' => 'values',
43+
'optional' => FALSE,
44+
'source' => 'data',
45+
'description' => 'The Signup data',
46+
'type' => 'array',
47+
),
4948
),
5049
),
5150
'reportback' => array(
@@ -155,12 +154,29 @@ function _campaign_resource_index($parameters) {
155154

156155
/**
157156
* Callback for Campaigns Signup targeted action.
157+
*
158+
* @param int $nid
159+
* The Node nid to post the signup to.
160+
* @param array $values
161+
* The signup data to post. Expected keys:
162+
* - uid: The user uid (int). Optional, uses global $user if not set.
163+
* - source (string).
158164
*/
159-
function _campaign_resource_signup($nid) {
165+
function _campaign_resource_signup($nid, $values) {
166+
global $user;
167+
if (!isset($values['uid'])) {
168+
$values['uid'] = $user->uid;
169+
}
170+
if (!isset($values['source'])) {
171+
$values['source'] = NULL;
172+
}
173+
if (DOSOMETHING_SIGNUP_LOG_SIGNUPS) {
174+
watchdog('dosomething_api', '_campaign_resource_signup values:' . json_encode($values));
175+
}
160176
// @todo: Pass parameter into signup_create whether or not to send SMS.
161177
// Since SMS campaign signups would hit this endpoint, would not want
162178
// to send an additional "You've signed up text".
163-
return dosomething_signup_create($nid);
179+
return dosomething_signup_create($nid, $values['uid'], $values['source']);
164180
}
165181

166182
/**

lib/modules/dosomething/dosomething_signup/dosomething_signup.features.user_permission.inc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ function dosomething_signup_user_default_permissions() {
2020
'module' => 'dosomething_signup',
2121
);
2222

23+
// Exported permission: 'edit any signup'.
24+
$permissions['edit any signup'] = array(
25+
'name' => 'edit any signup',
26+
'roles' => array(
27+
'administrator' => 'administrator',
28+
),
29+
'module' => 'dosomething_signup',
30+
);
31+
2332
// Exported permission: 'view any signup'.
2433
$permissions['view any signup'] = array(
2534
'name' => 'view any signup',

lib/modules/dosomething/dosomething_signup/dosomething_signup.info

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ dependencies[] = views
99
features[ctools][] = views:views_default:3.0
1010
features[features_api][] = api:2
1111
features[user_permission][] = administer third party communication
12+
features[user_permission][] = edit any signup
1213
features[user_permission][] = view any signup
1314
features[views_view][] = node_signups
1415
files[] = dosomething_signup.test
1516
files[] = includes/dosomething_signup.inc
17+
mtime = 1416603772

lib/modules/dosomething/dosomething_signup/dosomething_signup.module

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* @file
44
* Code for the dosomething_signup feature.
55
*/
6+
define('DOSOMETHING_SIGNUP_LOG_MOBILECOMMONS', variable_get('dosomething_signup_log_mobilecommons') ? TRUE : FALSE);
7+
define('DOSOMETHING_SIGNUP_LOG_SIGNUPS', variable_get('dosomething_signup_log_signups') ? TRUE : FALSE);
68

79
include_once 'dosomething_signup.features.inc';
810
include_once 'dosomething_signup.forms.inc';
@@ -11,9 +13,6 @@ include_once 'dosomething_signup.theme.inc';
1113
include_once 'includes/dosomething_signup.mobilecommons.inc';
1214
include_once 'includes/dosomething_signup.variable.inc';
1315

14-
DEFINE('DOSOMETHING_SIGNUP_LOG_MOBILECOMMONS', variable_get('dosomething_signup_log_mobilecommons') ? TRUE : FALSE);
15-
DEFINE('DOSOMETHING_SIGNUP_LOG_SIGNUPS', variable_get('dosomething_signup_log_signups') ? TRUE : FALSE);
16-
1716
/**
1817
* Implements hook_menu().
1918
*/
@@ -86,6 +85,10 @@ function dosomething_signup_permission() {
8685
'title' => t('Administer Third Party Communications'),
8786
'description' => t('Manage Mobile Commons/MailChimp Opt-in IDs.'),
8887
),
88+
'edit any signup' => array(
89+
'title' => t('Edit any signup'),
90+
'description' => t('Edit any signup.'),
91+
),
8992
'view any signup' => array(
9093
'title' => t('View any signup'),
9194
'description' => t('View any signup.'),
@@ -167,7 +170,10 @@ function dosomething_signup_create($nid, $uid = NULL, $source = NULL, $timestamp
167170
$entity = entity_create('signup', $values);
168171
// The SignupEntityController save method handles any NULL values.
169172
$entity->save();
170-
return $entity->sid;
173+
if (isset($entity->sid)) {
174+
return $entity->sid;
175+
}
176+
return FALSE;
171177
}
172178
catch (Exception $e) {
173179
// Keep message general in case a user ever sees it.

lib/modules/dosomething/dosomething_signup/includes/dosomething_signup.inc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ class SignupEntityController extends EntityAPIController {
7878
* Populates timestamp and uid automatically.
7979
*/
8080
public function save($entity, DatabaseTransaction $transaction = NULL) {
81+
global $user;
8182
if (isset($entity->is_new)) {
8283
if (!isset($entity->timestamp)) {
8384
$entity->timestamp = REQUEST_TIME;
@@ -87,6 +88,22 @@ class SignupEntityController extends EntityAPIController {
8788
$entity->uid = $user->uid;
8889
}
8990
}
91+
// Make sure a uid exists.
92+
if (!isset($entity->uid)) {
93+
return FALSE;
94+
}
95+
// If the entity uid doesnt belong to current user:
96+
if ($entity->uid != $user->uid) {
97+
// And current user can't edit any reportback:
98+
if (!user_access('edit any signup')) {
99+
watchdog('dosomething_signup', "Attempted uid override for @entity by User @uid",
100+
array(
101+
'@entity' => json_encode($entity),
102+
'@uid' => $user->uid,
103+
), WATCHDOG_WARNING);
104+
return FALSE;
105+
}
106+
}
90107
parent::save($entity, $transaction);
91108
if (DOSOMETHING_SIGNUP_LOG_SIGNUPS) {
92109
watchdog('dosomething_signup', json_encode($entity));

0 commit comments

Comments
 (0)