Skip to content

Commit

Permalink
ensure admin user component exists #231
Browse files Browse the repository at this point in the history
  • Loading branch information
nadar committed Oct 10, 2018
1 parent 7627fbd commit 9c3d828
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/behaviors/LogBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,21 @@ private function toJson($array)

return Json::encode($array);
}

/**
* Returns the user id for the current admin user if logged in and component is existsi.
*
* @return integer
* @since 1.2.3
*/
protected function getUserId()
{
if (Yii::$app->has('adminuser') && Yii::$app->adminuser->getIdentity()) {
return Yii::$app->adminuser->id;
}

return 0;
}

/**
* After delete event.
Expand All @@ -54,7 +69,7 @@ public function eventAfterDelete($event)
{
if (Yii::$app instanceof Application) {
Yii::$app->db->createCommand()->insert('admin_ngrest_log', [
'user_id' => is_null(Yii::$app->adminuser->getIdentity()) ? 0 : Yii::$app->adminuser->getId(),
'user_id' => $this->getUserId(),
'timestamp_create' => time(),
'route' => $this->route,
'api' => $this->api,
Expand All @@ -77,7 +92,7 @@ public function eventAfterInsert($event)
{
if (Yii::$app instanceof Application) {
Yii::$app->db->createCommand()->insert('admin_ngrest_log', [
'user_id' => is_null(Yii::$app->adminuser->getIdentity()) ? 0 : Yii::$app->adminuser->getId(),
'user_id' => $this->getUserId(),
'timestamp_create' => time(),
'route' => $this->route,
'api' => $this->api,
Expand All @@ -100,7 +115,7 @@ public function eventAfterUpdate($event)
{
if (Yii::$app instanceof Application) {
Yii::$app->db->createCommand()->insert('admin_ngrest_log', [
'user_id' => is_null(Yii::$app->adminuser->getIdentity()) ? 0 : Yii::$app->adminuser->getId(),
'user_id' => $this->getUserId(),
'timestamp_create' => time(),
'route' => $this->route,
'api' => $this->api,
Expand Down

0 comments on commit 9c3d828

Please sign in to comment.