Skip to content

Commit

Permalink
Clean code before release of v2.1RC1
Browse files Browse the repository at this point in the history
  • Loading branch information
jjrom committed Jul 16, 2015
1 parent 47066a2 commit 3f5d397
Show file tree
Hide file tree
Showing 17 changed files with 1,075 additions and 1,337 deletions.
119 changes: 97 additions & 22 deletions include/resto/Modules/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,6 @@
*/
class Admin extends RestoModule {

/*
* Reference to RestoAPI functions
*/
private $API;

/**
* Constructor
*
Expand All @@ -71,7 +66,6 @@ class Admin extends RestoModule {
*/
public function __construct($context, $user) {
parent::__construct($context, $user);
$this->API = new RestoAPI($context);
}

/**
Expand Down Expand Up @@ -195,9 +189,11 @@ private function GET_users($segments) {
* No {userid} => return all profiles
*/
if (!isset($segments[1])) {
return $this->API->getUsersProfiles();
return RestoLogUtil::success('Profiles for all users', array(
'profiles' => $this->context->dbDriver->get(RestoDatabaseDriver::USERS_PROFILES)
));
}

/*
* Get user
*/
Expand All @@ -207,7 +203,9 @@ private function GET_users($segments) {
* users/{userid}
*/
if (!isset($segments[2])) {
return $this->API->getUserProfile($user);
return RestoLogUtil::success('Profile for ' . $user->profile['email'], array(
'profile' => $user->profile
));
}

/*
Expand All @@ -217,35 +215,52 @@ private function GET_users($segments) {
if (isset($segments[3])) {
return RestoLogUtil::httpError(404);
}
return $this->API->getUserGroups($user);
return RestoLogUtil::success('Groups for ' . $user->profile['email'], array(
'email' => $user->profile['email'],
'groups' => $user->profile['groups']
));
}

/*
* users/{userid}/rights
*/
if ($segments[2] === 'rights') {
return $this->API->getUserRights($user, isset($segments[3]) ? $segments[3] : null, isset($segments[4]) ? $segments[4] : null);
return $this->getRights($user, isset($segments[3]) ? $segments[3] : null, isset($segments[4]) ? $segments[4] : null);
}

/*
* users/{userid}/cart
*/
if ($segments[2] === 'cart') {
return $this->API->getUserCart($user, isset($segments[3]) ? $segments[3] : null);
if ($segments[2] === 'cart' && !isset($segments[3])) {
return $this->user->getCart();
}

/*
* users/{userid}/orders
*/
if ($segments[2] === 'orders') {
return $this->API->getUserOrders($user, isset($segments[3]) ? $segments[3] : null);
if (isset($segments[3])) {
return new RestoOrder($user, $this->context, $segments[3]);
}
else {
return RestoLogUtil::success('Orders for user ' . $user->profile['email'], array(
'email' => $user->profile['email'],
'userid' => $user->profile['userid'],
'orders' => $user->getOrders()
));
}
}

/*
* users/{userid}/signatures
*/
if ($segments[2] === 'signatures') {
return $this->API->getUserSignatures($user, isset($segments[3]) ? $segments[3] : null);
if ($segments[2] === 'signatures' && !isset($segments[3])) {
return RestoLogUtil::success('Signatures for ' . $user->profile['email'], array(
'email' => $user->profile['email'],
'userid' => $user->profile['userid'],
'groups' => $user->profile['groups'],
'signatures' => $user->getUserSignatures()
));
}

return RestoLogUtil::httpError(404);
Expand All @@ -269,7 +284,7 @@ private function POST_licenses($segments, $data) {
RestoLogUtil::httpError(404);
}

return $this->API->createLicense($data);
return $this->createLicense($data);

}

Expand Down Expand Up @@ -298,7 +313,12 @@ private function POST_users($segments, $data) {
*/
$user = new RestoUser($this->context->dbDriver->get(RestoDatabaseDriver::USER_PROFILE, array('userid' => $segments[1])), $this->context);

return $this->API->setUserRights($user, $data['rights'], isset($segments[3]) ? $segments[3] : null, isset($segments[4]) ? $segments[4] : null);
/*
* Store/update rights
*/
$user->setRights($data['rights'], isset($segments[3]) ? $segments[3] : null, isset($segments[4]) ? $segments[4] : null);

return $this->getRights($user, isset($segments[3]) ? $segments[3] : null, isset($segments[4]) ? $segments[4] : null);

}

Expand Down Expand Up @@ -329,7 +349,7 @@ private function PUT_users($segments, $data) {
RestoLogUtil::httpError(400, 'Groups is not set');
}

return $this->API->addUserGroups($user, $data['groups']);
return $user->addGroups($data['groups']);

}

Expand All @@ -350,7 +370,10 @@ private function DELETE_licenses($segments) {
RestoLogUtil::httpError(404);
}

return $this->API->removeLicense($segments[1]);
$this->context->dbDriver->remove(RestoDatabaseDriver::LICENSE, array('licenseId' => $segments[1]));
return RestoLogUtil::success('License removed', array(
'licenseId' => $segments[1]
));

}

Expand Down Expand Up @@ -383,15 +406,67 @@ private function DELETE_users($segments) {
* users/{userid}/groups/{groups}
*/
if ($segments[2] === 'groups' && !empty($segments[3])) {
$this->API->removeUserGroups($user, $segments[3]);
return $user->removeGroups($user, $segments[3]);
}
/*
* users/{userid}/rights
*/
else if ($segments[2] !== 'rights') {
$this->API->removeUserRights($user, isset($segments[3]) ? $segments[3] : null, isset($segments[4]) ? $segments[4] : null);
$user->removeRights($user, isset($segments[3]) ? $segments[3] : null, isset($segments[4]) ? $segments[4] : null);
return $user->getRights(isset($segments[3]) ? $segments[3] : null, isset($segments[4]) ? $segments[4] : null);
}

RestoLogUtil::httpError(404);


}

/**
* Return formated rights
*
* @param RestoUser $user
* @param string $collectionName
* @param string $featureIdentifier
*/
private function getRights($user, $collectionName, $featureIdentifier) {
return RestoLogUtil::success('Rights for ' . $user->profile['email'], array(
'email' => $user->profile['email'],
'userid' => $user->profile['userid'],
'groups' => $user->profile['groups'],
'rights' => $user->getRights($collectionName, $featureIdentifier)
));

}

/**
* Create license
*
* @param array $data
*/
private function createLicense($data) {

if (!isset($data['licenseId'])) {
RestoLogUtil::httpError(400, 'license Identifier is not set');
}

$license = $this->context->dbDriver->store(RestoDatabaseDriver::LICENSE, array(
'license' => array(
'licenseId' => isset($data['licenseId']) ? $data['licenseId'] : null,
'grantedCountries' => isset($data['grantedCountries']) ? $data['grantedCountries'] : null,
'grantedOrganizationCountries' => isset($data['grantedOrganizationCountries']) ? $data['grantedOrganizationCountries'] : null,
'grantedFlags' => isset($data['grantedFlags']) ? $data['grantedFlags'] : null,
'viewService' => isset($data['viewService']) ? $data['viewService'] : null,
'hasToBeSigned' => isset($data['hasToBeSigned']) ? $data['hasToBeSigned'] : null,
'signatureQuota' => isset($data['signatureQuota']) ? $data['signatureQuota'] : -1,
'description' => isset($data['description']) ? $data['description'] : null
))
);
if (!isset($license)) {
RestoLogUtil::httpError(500, 'Database connection error');
}

return RestoLogUtil::success('license ' . $data['licenseId'] . ' created');
}


}
2 changes: 1 addition & 1 deletion include/resto/Modules/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ private function token($key) {
RestoLogUtil::httpError(401, 'Unauthorized');
}

return $this->context->createToken($user->profile['userid'], $user->profile);
return $this->context->createJWT($user->profile['userid'], $user->profile);

}

Expand Down
Loading

0 comments on commit 3f5d397

Please sign in to comment.