Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F120822576
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
20 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/src/app/Entitlement.php b/src/app/Entitlement.php
index f0090cf3..e277807f 100644
--- a/src/app/Entitlement.php
+++ b/src/app/Entitlement.php
@@ -1,106 +1,79 @@
<?php
-
namespace App;
use Illuminate\Database\Eloquent\Model;
/**
- The eloquent definition of an entitlement.
-
- Owned by a {@link \App\User}, billed to a {@link \App\Wallet}.
+ * The eloquent definition of an Entitlement.
+ *
+ * Owned by a {@link \App\User}, billed to a {@link \App\Wallet}.
*/
class Entitlement extends Model
{
+ /**
+ * This table does not use auto-increment.
+ *
+ * @var boolean
+ */
public $incrementing = false;
+
+ /**
+ * The key type is actually a string.
+ *
+ * @var string
+ */
protected $keyType = 'string';
+ /**
+ * The fillable columns for this Entitlement
+ *
+ * @var array
+ */
protected $fillable = [
'sku_id',
'owner_id',
'user_id',
'wallet_id',
'description'
];
/**
- Provide a custom ID (uuid) property.
-
- @return void
- */
- protected static function boot()
- {
- parent::boot();
-
- static::creating(
- function ($entitlement) {
- $entitlement->{$entitlement->getKeyName()} = \App\Utils::uuidStr();
-
- // Make sure the owner is at least a controller on the wallet
- $owner = \App\User::find($entitlement->owner_id);
- $wallet = \App\Wallet::find($entitlement->wallet_id);
-
- if (!$owner) {
- return false;
- }
-
- if (!$wallet) {
- return false;
- }
-
- if (!$wallet->owner() == $owner) {
- if (!$wallet->controllers->contains($owner)) {
- return false;
- }
- }
-
- $sku = \App\Sku::find($entitlement->sku_id);
-
- if (!$sku) {
- return false;
- }
-
- $wallet->debit($sku->cost);
- }
- );
- }
-
- /**
- The SKU concerned.
-
- @return Sku
+ * The SKU concerned.
+ *
+ * @return Sku
*/
public function sku()
{
return $this->belongsTo('App\Sku');
}
/**
- The owner of this entitlement.
-
- @return User
+ * The owner of this entitlement.
+ *
+ * @return User
*/
public function owner()
{
return $this->belongsTo('App\User', 'owner_id');
}
/**
- The target user for this entitlement
-
- @return User
+ * The target user for this entitlement
+ *
+ * @return User
*/
public function user()
{
return $this->belongsTo('App\User');
}
/**
- The wallet this entitlement is being billed to
-
- @return Wallet
+ * The wallet this entitlement is being billed to
+ *
+ * @return Wallet
*/
public function wallet()
{
return $this->belongsTo('App\Wallet');
}
}
diff --git a/src/app/Observers/EntitlementObserver.php b/src/app/Observers/EntitlementObserver.php
new file mode 100644
index 00000000..f9d1d9e8
--- /dev/null
+++ b/src/app/Observers/EntitlementObserver.php
@@ -0,0 +1,56 @@
+<?php
+
+namespace App\Observers;
+
+use App\Entitlement;
+
+/**
+ * This is an observer for the Entitlement model definition.
+ */
+class EntitlementObserver
+{
+ /**
+ * Handle the "creating" event on an Entitlement.
+ *
+ * Ensures that the entry uses a custom ID (uuid).
+ *
+ * Ensures that the {@link \App\Wallet} to which it is to be billed is owned or controlled by
+ * the {@link \App\User} entitled.
+ *
+ * @param Entitlement $entitlement The entitlement being created.
+ *
+ * @return void
+ */
+ public function creating(Entitlement $entitlement)
+ {
+ $entitlement->{$entitlement->getKeyName()} = \App\Utils::uuidStr();
+
+ // can't dispatch job here because it'll fail serialization
+
+ // Make sure the owner is at least a controller on the wallet
+ $owner = \App\User::find($entitlement->owner_id);
+ $wallet = \App\Wallet::find($entitlement->wallet_id);
+
+ if (!$owner) {
+ return false;
+ }
+
+ if (!$wallet) {
+ return false;
+ }
+
+ if (!$wallet->owner() == $owner) {
+ if (!$wallet->controllers->contains($owner)) {
+ return false;
+ }
+ }
+
+ $sku = \App\Sku::find($entitlement->sku_id);
+
+ if (!$sku) {
+ return false;
+ }
+
+ $wallet->debit($sku->cost);
+ }
+}
diff --git a/src/app/Observers/SkuObserver.php b/src/app/Observers/SkuObserver.php
new file mode 100644
index 00000000..fc4a75a4
--- /dev/null
+++ b/src/app/Observers/SkuObserver.php
@@ -0,0 +1,20 @@
+<?php
+
+namespace App\Observers;
+
+use App\Sku;
+
+class SkuObserver
+{
+ /**
+ * Ensure the SKU ID is a custom ID (uuid).
+ *
+ * @param Sku $sku
+ *
+ * @return void
+ */
+ public function creating(Sku $sku)
+ {
+ $sku->{$sku->getKeyName()} = \App\Utils::uuidStr();
+ }
+}
diff --git a/src/app/Observers/UserObserver.php b/src/app/Observers/UserObserver.php
index ac5612ec..8ffc44db 100644
--- a/src/app/Observers/UserObserver.php
+++ b/src/app/Observers/UserObserver.php
@@ -1,108 +1,92 @@
<?php
namespace App\Observers;
use App\User;
class UserObserver
{
/**
- * Handle the user "creating" event.
+ * Handle the "creating" event.
*
* Ensure that the user is created with a random, large integer.
*
* @param \App\User $user The user being created.
*
* @return void
*/
public function creating(User $user)
{
$user->{$user->getKeyName()} = \App\Utils::uuidInt();
// can't dispatch job here because it'll fail serialization
}
/**
- * Handle the user "created" event.
+ * Handle the "created" event.
+ *
+ * Ensures the user has at least one wallet.
+ *
+ * Should ensure some basic settings are available as well.
*
* @param \App\User $user The user created.
*
* @return void
*/
public function created(User $user)
{
// FIXME: Actual proper settings
$user->setSettings(
[
'country' => 'CH',
'currency' => 'CHF',
'first_name' => '',
'last_name' => '',
'billing_address' => '',
'organization' => ''
]
);
$user->wallets()->create();
\App\Jobs\ProcessUserCreate::dispatch($user);
}
/**
- * Handle the user "updated" event.
+ * Handle the "deleting" event.
*
- * @param \App\User $user
- * @return void
- */
- public function updated(User $user)
- {
- //
- }
-
- /**
- * Handle the user "deleted" event.
+ * @param User $user The user that is being deleted.
*
- * @param \App\User $user
* @return void
*/
- public function deleted(User $user)
- {
- }
-
public function deleting(User $user)
{
\App\Jobs\ProcessUserDelete::dispatch($user);
}
/**
- * Handle the user "restored" event.
+ * Handle the "retrieving" event.
+ *
+ * @param User $user The user that is being retrieved.
+ *
+ * @todo This is useful for audit.
*
- * @param \App\User $user
* @return void
*/
- public function restored(User $user)
- {
- //
- }
-
public function retrieving(User $user)
{
\App\Jobs\ProcessUserRead::dispatch($user);
}
- public function updating(User $user)
- {
- \App\Jobs\ProcessUserUpdate::dispatch($user);
- }
-
/**
- * Handle the user "force deleted" event.
+ * Handle the "updating" event.
+ *
+ * @param User $user The user that is being updated.
*
- * @param \App\User $user
* @return void
*/
- public function forceDeleted(User $user)
+ public function updating(User $user)
{
- //
+ \App\Jobs\ProcessUserUpdate::dispatch($user);
}
}
diff --git a/src/app/Observers/WalletObserver.php b/src/app/Observers/WalletObserver.php
new file mode 100644
index 00000000..1cb51fa5
--- /dev/null
+++ b/src/app/Observers/WalletObserver.php
@@ -0,0 +1,58 @@
+<?php
+
+namespace App\Observers;
+
+use App\Wallet;
+
+/**
+ * This is an observer for the Wallet model definition.
+ */
+class WalletObserver
+{
+ /**
+ * Ensure the wallet ID is a custom ID (uuid).
+ *
+ * @param Wallet $wallet
+ *
+ * @return void
+ */
+ public function creating(Wallet $wallet)
+ {
+ $wallet->{$wallet->getKeyName()} = \App\Utils::uuidStr();
+ }
+
+ /**
+ * Handle the wallet "deleting" event.
+ *
+ * Ensures that a wallet with a non-zero balance can not be deleted.
+ *
+ * Ensures that the wallet being deleted is not the last wallet for the user.
+ *
+ * Ensures that no entitlements are being billed to the wallet currently.
+ *
+ * @param Wallet $wallet The wallet being deleted.
+ *
+ * @return boolean|null
+ */
+ public function deleting(Wallet $wallet)
+ {
+ // can't delete a wallet that has any balance on it (positive and negative).
+ if ($wallet->balance != 0.00) {
+ return false;
+ }
+
+ if (!$wallet->owner) {
+ throw new \Exception("Wallet: " . var_export($wallet, true));
+ }
+
+ // can't remove the last wallet for the owner.
+ if ($wallet->owner->wallets()->count() <= 1) {
+ return false;
+ }
+
+ // can't remove a wallet that has billable entitlements attached.
+ if ($wallet->entitlements()->count() > 0) {
+ return false;
+ }
+ }
+}
diff --git a/src/app/Sku.php b/src/app/Sku.php
index 041d43e8..c8a631c3 100644
--- a/src/app/Sku.php
+++ b/src/app/Sku.php
@@ -1,46 +1,30 @@
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
/**
- The eloquent definition of a Stock Keeping Unit (SKU).
+ * The eloquent definition of a Stock Keeping Unit (SKU).
*/
class Sku extends Model
{
public $incrementing = false;
protected $keyType = 'string';
protected $casts = [
'cost' => 'float',
];
protected $fillable = ['title', 'description', 'cost'];
- /**
- Provide a custom ID (uuid) property.
-
- @return void
- */
- protected static function boot()
- {
- parent::boot();
-
- static::creating(
- function ($sku) {
- $sku->{$sku->getKeyName()} = \App\Utils::uuidStr();
- }
- );
- }
-
/**
List the entitlements that consume this SKU.
@return Entitlement[]
*/
public function entitlements()
{
return $this->hasMany('App\Entitlement');
}
}
diff --git a/src/app/User.php b/src/app/User.php
index 22194ff3..e089135e 100644
--- a/src/app/User.php
+++ b/src/app/User.php
@@ -1,132 +1,135 @@
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Iatstuti\Database\Support\NullableFields;
use Tymon\JWTAuth\Contracts\JWTSubject;
use App\Traits\UserSettingsTrait;
+/**
+ * The eloquent definition of a User.
+ */
class User extends Authenticatable implements JWTSubject
{
use Notifiable;
use NullableFields;
use UserSettingsTrait;
// change the default primary key type
public $incrementing = false;
protected $keyType = 'bigint';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password', 'password_ldap'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'password_ldap', 'remember_token',
];
protected $nullable = [
'name',
'password',
'password_ldap'
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
/**
* Any wallets on which this user is a controller.
*
* @return Wallet[]
*/
public function accounts()
{
return $this->belongsToMany(
'App\Wallet', // The foreign object definition
'user_accounts', // The table name
'user_id', // The local foreign key
'wallet_id' // The remote foreign key
);
}
/**
* Entitlements for this user.
*
* @return Entitlement[]
*/
public function entitlements()
{
return $this->hasMany('App\Entitlement');
}
public function addEntitlement($entitlement)
{
if (!$this->entitlements()->get()->contains($entitlement)) {
return $this->entitlements()->save($entitlement);
}
}
public function settings()
{
return $this->hasMany('App\UserSetting', 'user_id');
}
/**
* Wallets this user owns.
*
* @return Wallet[]
*/
public function wallets()
{
return $this->hasMany('App\Wallet');
}
public function getJWTIdentifier()
{
return $this->getKey();
}
public function getJWTCustomClaims()
{
return [];
}
public function setPasswordAttribute($password)
{
if (!empty($password)) {
$this->attributes['password'] = bcrypt($password, [ "rounds" => 12 ]);
$this->attributes['password_ldap'] = '{SSHA512}' . base64_encode(
pack('H*', hash('sha512', $password))
);
}
}
public function setPasswordLdapAttribute($password)
{
if (!empty($password)) {
$this->attributes['password'] = bcrypt($password, [ "rounds" => 12 ]);
$this->attributes['password_ldap'] = '{SSHA512}' . base64_encode(
pack('H*', hash('sha512', $password))
);
}
}
}
diff --git a/src/app/Wallet.php b/src/app/Wallet.php
index 5f29a297..dae8e63d 100644
--- a/src/app/Wallet.php
+++ b/src/app/Wallet.php
@@ -1,208 +1,140 @@
<?php
namespace App;
use Iatstuti\Database\Support\NullableFields;
use Illuminate\Database\Eloquent\Model;
/**
- The eloquent definition of a wallet -- a container with a chunk of change.
-
- A wallet is owned by an {@link \App\User}.
+ * The eloquent definition of a wallet -- a container with a chunk of change.
+ *
+ * A wallet is owned by an {@link \App\User}.
*/
class Wallet extends Model
{
use NullableFields;
/**
Our table name for the shall be 'wallet'.
@var string
*/
- /**
- {@inheritDoc}
- */
public $incrementing = false;
- /**
- {@inheritDoc}
- */
protected $keyType = 'string';
- /**
- {@inheritDoc}
- */
public $timestamps = false;
- /**
- {@inheritDoc}
- */
protected $attributes = [
'balance' => 0.00,
'currency' => 'CHF'
];
- /**
- {@inheritDoc}
- */
protected $fillable = [
'currency'
];
- /**
- {@inheritDoc}
- */
protected $nullable = [
'description'
];
- /**
- {@inheritDoc}
- */
protected $casts = [
'balance' => 'float',
];
- /**
- {@inheritDoc}
- */
protected $guarded = ['balance'];
/**
- Provide a custom ID (uuid) property.
-
- @todo migrate to observers
-
- @return void
- */
- protected static function boot()
- {
- parent::boot();
-
- // retrieved, creating, created, updating, updated, saving, saved, deleting, deleted,
- // restoring, restored
- static::creating(
- function ($wallet) {
- $wallet->{$wallet->getKeyName()} = \App\Utils::uuidStr();
- }
- );
-
- // Prevent a wallet with a positive of negative balance from being deleted.
- static::deleting(
- function ($wallet) {
- // can't delete a wallet that has any balance on it (positive and negative).
- if ($wallet->balance != 0.00) {
- return false;
- }
-
- if (!$wallet->owner) {
- throw new \Exception("Wallet: " . var_export($wallet, true));
- }
-
- // can't remove the last wallet for the owner.
- if ($wallet->owner->wallets()->count() <= 1) {
- return false;
- }
-
- // can't remove a wallet that has billable entitlements attached.
- if ($wallet->entitlements()->count() > 0) {
- return false;
- }
- }
- );
- }
-
- /**
- Add a controller to this wallet.
-
- @param User $user The user to add as a controller to this wallet.
-
- @return void
+ * Add a controller to this wallet.
+ *
+ * @param User $user The user to add as a controller to this wallet.
+ *
+ * @return void
*/
public function addController($user)
{
if (!$this->controllers()->get()->contains($user)) {
return $this->controllers()->save($user);
}
}
/**
- Remove a controller from this wallet.
-
- @param User $user The user to remove as a controller from this wallet.
-
- @return void
+ * Remove a controller from this wallet.
+ *
+ * @param User $user The user to remove as a controller from this wallet.
+ *
+ * @return void
*/
public function removeController($user)
{
if ($this->controllers()->get()->contains($user)) {
return $this->controllers()->detach($user);
}
}
/**
- Add an amount of pecunia to this wallet's balance.
-
- @param float $amount The amount of pecunia to add.
-
- @return Wallet
+ * Add an amount of pecunia to this wallet's balance.
+ *
+ * @param float $amount The amount of pecunia to add.
+ *
+ * @return Wallet
*/
public function credit(float $amount)
{
$this->balance += $amount;
$this->save();
return $this;
}
/**
- Deduct an amount of pecunia from this wallet's balance.
-
- @param float $amount The amount of pecunia to deduct.
-
- @return Wallet
+ * Deduct an amount of pecunia from this wallet's balance.
+ *
+ * @param float $amount The amount of pecunia to deduct.
+ *
+ * @return Wallet
*/
public function debit(float $amount)
{
$this->balance -= $amount;
$this->save();
return $this;
}
/**
- Controllers of this wallet.
-
- @return User[]
+ * Controllers of this wallet.
+ *
+ * @return User[]
*/
public function controllers()
{
return $this->belongsToMany(
'App\User', // The foreign object definition
'user_accounts', // The table name
'wallet_id', // The local foreign key
'user_id' // The remote foreign key
);
}
/**
- Entitlements billed to this wallet.
-
- @return Entitlement[]
+ * Entitlements billed to this wallet.
+ *
+ * @return Entitlement[]
*/
public function entitlements()
{
return $this->hasMany('App\Entitlement');
}
/**
- The owner of the wallet -- the wallet is in his/her back pocket.
-
- @return User
+ * The owner of the wallet -- the wallet is in his/her back pocket.
+ *
+ * @return User
*/
public function owner()
{
return $this->belongsTo('App\User', 'user_id', 'id');
}
}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Fri, Apr 24, 9:52 AM (1 w, 21 h ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
18896230
Default Alt Text
(20 KB)
Attached To
Mode
rK kolab
Attached
Detach File
Event Timeline