Skip to content

Commit

Permalink
Merge pull request #164 from JarvusInnovations/develop
Browse files Browse the repository at this point in the history
Release: emergence-skeleton v1.3.11
  • Loading branch information
themightychris authored Aug 30, 2018
2 parents c51a30d + 2d232dd commit 43c3d83
Showing 1 changed file with 47 additions and 2 deletions.
49 changes: 47 additions & 2 deletions php-classes/ActiveRecord.class.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Emergence\People\IPerson;

class ActiveRecord
{
// configurables
Expand Down Expand Up @@ -114,6 +116,9 @@ class ActiveRecord
),
'recordURL' => array(
'getter' => 'getURL'
),
'availableActions' => array(
'getter' => 'getAvailableActions'
)
);

Expand Down Expand Up @@ -219,7 +224,7 @@ public function __construct($record = array(), $isDirty = false, $isPhantom = nu

// authorize read access
if (!$this->userCanReadRecord()) {
throw new UserUnauthorizedException('Read authorization denied');
throw new UserUnauthorizedException('read authorization denied');
}

// set Class
Expand Down Expand Up @@ -435,7 +440,34 @@ public function getThumbnailURL($width, $height = null, $exactSize = true)
return null;
}

public function userCanReadRecord(Emergence\People\IPerson $User = null)
public function getAvailableActions(IPerson $User = null)
{
$User = $User ?: $this->getUserFromEnvironment();

return array(
'create' => $this->userCanCreateRecord($User),
'read' => $this->userCanReadRecord($User),
'update' => $this->userCanUpdateRecord($User),
'delete' => $this->userCanDeleteRecord($User),
);
}

public function userCanCreateRecord(IPerson $User = null)
{
return true;
}

public function userCanReadRecord(IPerson $User = null)
{
return true;
}

public function userCanUpdateRecord(IPerson $User = null)
{
return true;
}

public function userCanDeleteRecord(IPerson $User = null)
{
return true;
}
Expand Down Expand Up @@ -780,6 +812,14 @@ public function dumpData($exit = false)
protected $_isSaving = false;
public function save($deep = true)
{
// authorize create/update access
if ($this->_isPhantom && !$this->userCanCreateRecord()) {
throw new UserUnauthorizedException('create authorization denied');
} elseif (!$this->_isPhantom && !$this->userCanUpdateRecord()) {
throw new UserUnauthorizedException('update authorization denied');
}

// prevent concurrent operations
if ($this->_isSaving) {
return null;
}
Expand Down Expand Up @@ -1135,6 +1175,11 @@ public function destroy()

public static function delete($id)
{
// authorize delete access
if (!$this->userCanDeleteRecord()) {
throw new UserUnauthorizedException('delete authorization denied');
}

DB::nonQuery('DELETE FROM `%s` WHERE `%s` = %u', array(
static::$tableName
,static::_cn('ID')
Expand Down

0 comments on commit 43c3d83

Please sign in to comment.