Skip to content

Commit

Permalink
added mailToken
Browse files Browse the repository at this point in the history
  • Loading branch information
hiqsol committed Oct 22, 2016
1 parent fb3a67c commit 8803de1
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions yii2/Service.php
Expand Up @@ -13,6 +13,7 @@

use hiqdev\php\confirmator\ServiceTrait;
use Yii;
use yii\helpers\Inflector;

class Service extends \yii\base\Component
{
Expand All @@ -37,4 +38,33 @@ public function getStorage()

return $this->_storage;
}

public function mailToken($user, $action, array $data = [])
{
if (!$user) {
return false;
}

if (Yii::$app->has('authManager')) {
$auth = Yii::$app->authManager;
if ($auth->getItem($action) && !$auth->checkAccess($user->id, $action)) {
return false;
}
}

$token = $this->issueToken(array_merge([
'action' => $action,
'email' => $user->email,
'username' => $user->username,
'notAfter' => '+ 3 days',
], $data));

$view = lcfirst(Inflector::id2camel($action . '-token'));

return Yii::$app->mailer->compose()
->renderHtmlBody($view, compact('user', 'token'))
->setTo([$user->email => $user->name])
->send()
;
}
}

0 comments on commit 8803de1

Please sign in to comment.