Renamed 'ip' column to 'subscribed_ip'
- updated code based on PR review
This commit is contained in:
@ -134,7 +134,7 @@ class Migrator {
|
|||||||
'last_name tinytext NOT NULL DEFAULT "",',
|
'last_name tinytext NOT NULL DEFAULT "",',
|
||||||
'email varchar(150) NOT NULL,',
|
'email varchar(150) NOT NULL,',
|
||||||
'status varchar(12) NOT NULL DEFAULT "' . Subscriber::STATUS_UNCONFIRMED . '",',
|
'status varchar(12) NOT NULL DEFAULT "' . Subscriber::STATUS_UNCONFIRMED . '",',
|
||||||
'ip varchar(32) NULL,',
|
'subscribed_ip varchar(32) NULL,',
|
||||||
'confirmed_ip varchar(32) NULL,',
|
'confirmed_ip varchar(32) NULL,',
|
||||||
'confirmed_at TIMESTAMP NULL,',
|
'confirmed_at TIMESTAMP NULL,',
|
||||||
'created_at TIMESTAMP NULL,',
|
'created_at TIMESTAMP NULL,',
|
||||||
|
@ -160,18 +160,18 @@ class Subscriber extends Model {
|
|||||||
'signup_confirmation.enabled'
|
'signup_confirmation.enabled'
|
||||||
);
|
);
|
||||||
|
|
||||||
// set user ip
|
$subscriber_data['subscribed_ip'] = (isset($_SERVER['REMOTE_ADDR']))
|
||||||
$subscriber_data['ip'] = (isset($_SERVER['REMOTE_ADDR']))
|
|
||||||
? $_SERVER['REMOTE_ADDR']
|
? $_SERVER['REMOTE_ADDR']
|
||||||
: null;
|
: null;
|
||||||
|
|
||||||
// make sure we don't allow too many subscriptions with the same ip address
|
// make sure we don't allow too many subscriptions with the same ip address
|
||||||
$subscription_count = Subscriber::where('ip', $subscriber_data['ip'])
|
$subscription_count = Subscriber::where(
|
||||||
->whereRaw(
|
'subscribed_ip',
|
||||||
|
$subscriber_data['subscribed_ip']
|
||||||
|
)->whereRaw(
|
||||||
'TIME_TO_SEC(TIMEDIFF(NOW(), created_at)) < ?',
|
'TIME_TO_SEC(TIMEDIFF(NOW(), created_at)) < ?',
|
||||||
self::SUBSCRIPTION_LIMIT_COOLDOWN
|
self::SUBSCRIPTION_LIMIT_COOLDOWN
|
||||||
)
|
)->count();
|
||||||
->count();
|
|
||||||
|
|
||||||
if($subscription_count > 0) {
|
if($subscription_count > 0) {
|
||||||
throw new \Exception(__('You need to wait before subscribing again.', 'mailpoet'));
|
throw new \Exception(__('You need to wait before subscribing again.', 'mailpoet'));
|
||||||
@ -180,7 +180,6 @@ class Subscriber extends Model {
|
|||||||
$subscriber = self::findOne($subscriber_data['email']);
|
$subscriber = self::findOne($subscriber_data['email']);
|
||||||
|
|
||||||
if($subscriber === false) {
|
if($subscriber === false) {
|
||||||
|
|
||||||
// create new subscriber
|
// create new subscriber
|
||||||
$subscriber = self::createOrUpdate($subscriber_data);
|
$subscriber = self::createOrUpdate($subscriber_data);
|
||||||
if($subscriber->getErrors() !== false) {
|
if($subscriber->getErrors() !== false) {
|
||||||
|
@ -58,12 +58,9 @@ class Pages {
|
|||||||
|
|
||||||
function confirm() {
|
function confirm() {
|
||||||
if($this->subscriber !== false) {
|
if($this->subscriber !== false) {
|
||||||
// update status
|
|
||||||
$this->subscriber->status = Subscriber::STATUS_SUBSCRIBED;
|
$this->subscriber->status = Subscriber::STATUS_SUBSCRIBED;
|
||||||
// set confirmed ip & time
|
|
||||||
$this->subscriber->confirmed_ip = $_SERVER['REMOTE_ADDR'];
|
$this->subscriber->confirmed_ip = $_SERVER['REMOTE_ADDR'];
|
||||||
$this->subscriber->setExpr('confirmed_at', 'NOW()');
|
$this->subscriber->setExpr('confirmed_at', 'NOW()');
|
||||||
|
|
||||||
$this->subscriber->save();
|
$this->subscriber->save();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -361,26 +361,34 @@ class SubscribersTest extends MailPoetTest {
|
|||||||
expect($response->errors[0]['message'])->contains('has no method');
|
expect($response->errors[0]['message'])->contains('has no method');
|
||||||
}
|
}
|
||||||
|
|
||||||
function testItCanSubscribeAUser() {
|
function testItCannotSubscribeWithoutSegments() {
|
||||||
// set ip address
|
|
||||||
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
|
|
||||||
|
|
||||||
$router = new Subscribers();
|
$router = new Subscribers();
|
||||||
|
|
||||||
// missing segment
|
|
||||||
$response = $router->subscribe(array(
|
$response = $router->subscribe(array(
|
||||||
'email' => 'toto@mailpoet.com'
|
'email' => 'toto@mailpoet.com'
|
||||||
|
// no segments specified
|
||||||
));
|
));
|
||||||
|
|
||||||
expect($response->status)->equals(APIResponse::STATUS_BAD_REQUEST);
|
expect($response->status)->equals(APIResponse::STATUS_BAD_REQUEST);
|
||||||
expect($response->errors[0]['message'])->equals('Please select a list.');
|
expect($response->errors[0]['message'])->equals('Please select a list.');
|
||||||
|
}
|
||||||
|
|
||||||
// proper subscription
|
function testItCanSubscribe() {
|
||||||
|
$router = new Subscribers();
|
||||||
$response = $router->subscribe(array(
|
$response = $router->subscribe(array(
|
||||||
'email' => 'toto@mailpoet.com',
|
'email' => 'toto@mailpoet.com',
|
||||||
'segments' => array($this->segment_1->id, $this->segment_2->id)
|
'segments' => array($this->segment_1->id, $this->segment_2->id)
|
||||||
));
|
));
|
||||||
expect($response->status)->equals(APIResponse::STATUS_OK);
|
expect($response->status)->equals(APIResponse::STATUS_OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItCannotMassSubscribe() {
|
||||||
|
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
|
||||||
|
|
||||||
|
$router = new Subscribers();
|
||||||
|
$response = $router->subscribe(array(
|
||||||
|
'email' => 'toto@mailpoet.com',
|
||||||
|
'segments' => array($this->segment_1->id, $this->segment_2->id)
|
||||||
|
));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$response = $router->subscribe(array(
|
$response = $router->subscribe(array(
|
||||||
|
Reference in New Issue
Block a user