Scrutinizer Auto-Fixes

This commit consists of patches automatically generated for this project on https://scrutinizer-ci.com
This commit is contained in:
Scrutinizer Auto-Fixer 2016-10-07 23:01:16 +00:00
parent cee3ea9865
commit 33ef75ac2f
5 changed files with 147 additions and 115 deletions

View file

@ -16,81 +16,81 @@ use OCP\IUser;
use OCA\Passman\Utility\Utils; use OCA\Passman\Utility\Utils;
class SharingACLMapper extends Mapper { class SharingACLMapper extends Mapper {
const TABLE_NAME = '*PREFIX*passman_sharing_acl'; const TABLE_NAME = '*PREFIX*passman_sharing_acl';
public function __construct(IDBConnection $db, Utils $utils) { public function __construct(IDBConnection $db, Utils $utils) {
parent::__construct($db, 'passman_sharing_acl'); parent::__construct($db, 'passman_sharing_acl');
$this->utils = $utils; $this->utils = $utils;
} }
/** /**
* Gets all the credential data for the given user * Gets all the credential data for the given user
* @param $userId * @param $userId
* @param $item_guid * @param $item_guid
* @return SharingACL[] * @return SharingACL[]
*/ */
public function getCredentialPermissions(IUser $userId, $item_guid){ public function getCredentialPermissions(IUser $userId, $item_guid){
$sql = "SELECT * FROM ". self::TABLE_NAME ." WHERE user_id = ? AND item_guid = ?"; $sql = "SELECT * FROM ". self::TABLE_NAME ." WHERE user_id = ? AND item_guid = ?";
return $this->findEntities($sql, [$userId, $item_guid]); return $this->findEntities($sql, [$userId, $item_guid]);
} }
public function createACLEntry(SharingACL $acl){ public function createACLEntry(SharingACL $acl){
return $this->insert($acl); return $this->insert($acl);
} }
/** /**
* Gets the currently accepted share requests from the given user for the given vault guid * Gets the currently accepted share requests from the given user for the given vault guid
* @param $user_id * @param $user_id
* @param $vault_id * @param $vault_id
* @return SharingACL[] * @return SharingACL[]
*/ */
public function getVaultEntries($user_id, $vault_id) { public function getVaultEntries($user_id, $vault_id) {
$q = "SELECT * FROM ". self::TABLE_NAME ." WHERE user_id = ? AND vault_guid = ?"; $q = "SELECT * FROM ". self::TABLE_NAME ." WHERE user_id = ? AND vault_guid = ?";
return $this->findEntities($q, [$user_id, $vault_id]); return $this->findEntities($q, [$user_id, $vault_id]);
} }
/** /**
* Gets the acl for a given item guid * Gets the acl for a given item guid
* @param $user_id * @param $user_id
* @param $item_guid * @param $item_guid
* @return SharingACL * @return SharingACL
*/ */
public function getItemACL($user_id, $item_guid) { public function getItemACL($user_id, $item_guid) {
$q = "SELECT * FROM " . self::TABLE_NAME . " WHERE item_guid = ? AND "; $q = "SELECT * FROM " . self::TABLE_NAME . " WHERE item_guid = ? AND ";
$filter = [$item_guid]; $filter = [$item_guid];
if ($user_id === null){ if ($user_id === null){
$q .= 'user_id is null'; $q .= 'user_id is null';
} }
else { else {
$q .= 'user_id = ? '; $q .= 'user_id = ? ';
$filter[] = $user_id; $filter[] = $user_id;
} }
return $this->findEntity($q, $filter); return $this->findEntity($q, $filter);
} }
/** /**
* Update the acl for a given item guid * Update the acl for a given item guid
* @param $user_id * @param $user_id
* @param $item_guid * @param $item_guid
* @return SharingACL * @return SharingACL
*/ */
public function updateCredentialACL(SharingACL $sharingACL) { public function updateCredentialACL(SharingACL $sharingACL) {
return $this->update($sharingACL); return $this->update($sharingACL);
} }
/** /**
* Gets the currently accepted share requests from the given user for the given vault guid * Gets the currently accepted share requests from the given user for the given vault guid
* @param $user_id * @param $user_id
* @param $vault_id * @param $vault_id
* @return SharingACL[] * @return SharingACL[]
*/ */
public function getCredentialAclList($item_guid) { public function getCredentialAclList($item_guid) {
$q = "SELECT * FROM ". self::TABLE_NAME ." WHERE item_guid = ?"; $q = "SELECT * FROM ". self::TABLE_NAME ." WHERE item_guid = ?";
return $this->findEntities($q, [$item_guid]); return $this->findEntities($q, [$item_guid]);
} }
public function deleteShareACL(SharingACL $ACL){ public function deleteShareACL(SharingACL $ACL){
return $this->delete($ACL); return $this->delete($ACL);
} }
} }

View file

@ -23,11 +23,11 @@ use OCA\Passman\Db\CredentialMapper;
class CredentialService { class CredentialService {
private $credentialMapper; private $credentialMapper;
private $sharingACL; private $sharingACL;
public function __construct(CredentialMapper $credentialMapper, SharingACLMapper $sharingACL) { public function __construct(CredentialMapper $credentialMapper, SharingACLMapper $sharingACL) {
$this->credentialMapper = $credentialMapper; $this->credentialMapper = $credentialMapper;
$this->sharingACL = $sharingACL; $this->sharingACL = $sharingACL;
} }
/** /**
@ -43,30 +43,46 @@ class CredentialService {
public function updateCredential($credential) { public function updateCredential($credential) {
return $this->credentialMapper->updateCredential($credential); return $this->credentialMapper->updateCredential($credential);
} }
/**
* @param Credential $credential
*/
public function upd($credential) { public function upd($credential) {
return $this->credentialMapper->upd($credential); return $this->credentialMapper->upd($credential);
} }
/**
* @param Credential $credential
*/
public function deleteCredential($credential){ public function deleteCredential($credential){
return $this->credentialMapper->deleteCredential($credential); return $this->credentialMapper->deleteCredential($credential);
} }
/**
* @param integer $vault_id
*/
public function getCredentialsByVaultId($vault_id, $user_id) { public function getCredentialsByVaultId($vault_id, $user_id) {
return $this->credentialMapper->getCredentialsByVaultId($vault_id, $user_id); return $this->credentialMapper->getCredentialsByVaultId($vault_id, $user_id);
} }
/**
* @param integer $vault_id
*/
public function getRandomCredentialByVaultId($vault_id, $user_id) { public function getRandomCredentialByVaultId($vault_id, $user_id) {
$credentials = $this->credentialMapper->getRandomCredentialByVaultId($vault_id, $user_id); $credentials = $this->credentialMapper->getRandomCredentialByVaultId($vault_id, $user_id);
return array_pop($credentials); return array_pop($credentials);
} }
/**
* @param integer $timestamp
*/
public function getExpiredCredentials($timestamp) { public function getExpiredCredentials($timestamp) {
return $this->credentialMapper->getExpiredCredentials($timestamp); return $this->credentialMapper->getExpiredCredentials($timestamp);
} }
public function getCredentialById($credential_id, $user_id){ public function getCredentialById($credential_id, $user_id) {
$credential = $this->credentialMapper->getCredentialById($credential_id); $credential = $this->credentialMapper->getCredentialById($credential_id);
if ($credential->getUserId() === $user_id){ if ($credential->getUserId() === $user_id) {
return $credential; return $credential;
} }
else { else {
@ -77,6 +93,10 @@ class CredentialService {
throw new DoesNotExistException("Did expect one result but found none when executing"); throw new DoesNotExistException("Did expect one result but found none when executing");
} }
/**
* @param integer $credential_id
*/
public function getCredentialLabelById($credential_id){ public function getCredentialLabelById($credential_id){
return $this->credentialMapper->getCredentialLabelById($credential_id); return $this->credentialMapper->getCredentialLabelById($credential_id);
} }

View file

@ -37,7 +37,7 @@ class CronService {
public function expireCredentials() { public function expireCredentials() {
$this->logger->info('Passman cron test', array('app' => 'passman')); $this->logger->info('Passman cron test', array('app' => 'passman'));
$expired_credentials = $this->credentialService->getExpiredCredentials($this->utils->getTime()); $expired_credentials = $this->credentialService->getExpiredCredentials($this->utils->getTime());
foreach($expired_credentials as $credential){ foreach ($expired_credentials as $credential) {
$link = ''; // @TODO create direct link to credential $link = ''; // @TODO create direct link to credential
$sql = 'SELECT count(*) as rows from `*PREFIX*notifications` WHERE `subject`= \'credential_expired\' AND object_id=?'; $sql = 'SELECT count(*) as rows from `*PREFIX*notifications` WHERE `subject`= \'credential_expired\' AND object_id=?';
@ -45,9 +45,9 @@ class CronService {
$id = $credential->getId(); $id = $credential->getId();
$query->bindParam(1, $id, \PDO::PARAM_INT); $query->bindParam(1, $id, \PDO::PARAM_INT);
$result = $query->execute(); $result = $query->execute();
$this->logger->debug($credential->getLabel() .' is expired, checking notifications!', array('app' => 'passman')); $this->logger->debug($credential->getLabel() . ' is expired, checking notifications!', array('app' => 'passman'));
if($result->fetchRow()['rows'] === 0) { if ($result->fetchRow()['rows'] === 0) {
$this->logger->debug($credential->getLabel() .' is expired, adding notification!', array('app' => 'passman')); $this->logger->debug($credential->getLabel() . ' is expired, adding notification!', array('app' => 'passman'));
$this->activityService->add( $this->activityService->add(
Activity::SUBJECT_ITEM_EXPIRED, array($credential->getLabel(), $credential->getUserId()), Activity::SUBJECT_ITEM_EXPIRED, array($credential->getLabel(), $credential->getUserId()),
@ -55,7 +55,7 @@ class CronService {
$link, $credential->getUserId(), Activity::TYPE_ITEM_EXPIRED); $link, $credential->getUserId(), Activity::TYPE_ITEM_EXPIRED);
$this->notificationService->credentialExpiredNotification($credential); $this->notificationService->credentialExpiredNotification($credential);
} else { } else {
$this->logger->debug($credential->getLabel() .' is expired, already notified!', array('app' => 'passman')); $this->logger->debug($credential->getLabel() . ' is expired, already notified!', array('app' => 'passman'));
} }
} }

View file

@ -71,7 +71,9 @@ class ShareService {
} }
public function createACLEntry(SharingACL $acl) { public function createACLEntry(SharingACL $acl) {
if ($acl->getCreated() === null) $acl->setCreated((new \DateTime())->getTimestamp()); if ($acl->getCreated() === null) {
$acl->setCreated((new \DateTime())->getTimestamp());
}
return $this->sharingACL->createACLEntry($acl); return $this->sharingACL->createACLEntry($acl);
} }
@ -124,12 +126,16 @@ class ShareService {
$return = []; $return = [];
foreach ($entries as $entry) { foreach ($entries as $entry) {
// Check if the user can read the credential, probably unnecesary, but just to be sure // Check if the user can read the credential, probably unnecesary, but just to be sure
if (!$entry->hasPermission(SharingACL::READ)) continue; if (!$entry->hasPermission(SharingACL::READ)) {
continue;
}
$tmp = $entry->jsonSerialize(); $tmp = $entry->jsonSerialize();
$tmp['credential_data'] = $this->credential->getCredentialById($entry->getItemId())->jsonSerialize(); $tmp['credential_data'] = $this->credential->getCredentialById($entry->getItemId())->jsonSerialize();
if (!$entry->hasPermission(SharingACL::FILES)) unset($tmp['credential_data']['files']); if (!$entry->hasPermission(SharingACL::FILES)) {
unset($tmp['credential_data']['files']);
}
unset($tmp['credential_data']['shared_key']); unset($tmp['credential_data']['shared_key']);
$return[] = $tmp; $return[] = $tmp;
} }
@ -150,12 +156,16 @@ class ShareService {
$acl = $this->sharingACL->getItemACL($user_id, $item_guid); $acl = $this->sharingACL->getItemACL($user_id, $item_guid);
// Check if the user can read the credential, probably unnecesary, but just to be sure // Check if the user can read the credential, probably unnecesary, but just to be sure
if (!$acl->hasPermission(SharingACL::READ)) throw new DoesNotExistException("Item not found or wrong access level"); if (!$acl->hasPermission(SharingACL::READ)) {
throw new DoesNotExistException("Item not found or wrong access level");
}
$tmp = $acl->jsonSerialize(); $tmp = $acl->jsonSerialize();
$tmp['credential_data'] = $this->credential->getCredentialById($acl->getItemId())->jsonSerialize(); $tmp['credential_data'] = $this->credential->getCredentialById($acl->getItemId())->jsonSerialize();
if (!$acl->hasPermission(SharingACL::FILES)) unset($tmp['credential_data']['files']); if (!$acl->hasPermission(SharingACL::FILES)) {
unset($tmp['credential_data']['files']);
}
unset($tmp['credential_data']['shared_key']); unset($tmp['credential_data']['shared_key']);
return $tmp; return $tmp;
@ -170,7 +180,9 @@ class ShareService {
*/ */
public function getItemHistory($user_id, $item_guid) { public function getItemHistory($user_id, $item_guid) {
$acl = $this->sharingACL->getItemACL($user_id, $item_guid); $acl = $this->sharingACL->getItemACL($user_id, $item_guid);
if (!$acl->hasPermission(SharingACL::READ | SharingACL::HISTORY)) return []; if (!$acl->hasPermission(SharingACL::READ | SharingACL::HISTORY)) {
return [];
}
return $this->revisions->getRevisions($acl->getItemId()); return $this->revisions->getRevisions($acl->getItemId());
} }

View file

@ -13,40 +13,40 @@ namespace OCA\Passman\Utility;
use OCP\AppFramework\Db\Entity; use OCP\AppFramework\Db\Entity;
class PermissionEntity extends Entity { class PermissionEntity extends Entity {
CONST READ = 0b00000001; CONST READ = 0b00000001;
CONST WRITE = 0b00000010; CONST WRITE = 0b00000010;
CONST FILES = 0b00000100; CONST FILES = 0b00000100;
CONST HISTORY = 0b00001000; CONST HISTORY = 0b00001000;
CONST OWNER = 0b10000000; CONST OWNER = 0b10000000;
/** /**
* Checks wether a user matches one or more permissions at once * Checks wether a user matches one or more permissions at once
* @param $permission * @param $permission
* @return bool * @return bool
*/ */
public function hasPermission($permission) { public function hasPermission($permission) {
$tmp = $this->getPermissions(); $tmp = $this->getPermissions();
$tmp = $tmp & $permission; $tmp = $tmp & $permission;
return $tmp === $permission; return $tmp === $permission;
} }
/** /**
* Adds the given permission or permissions set to the user current permissions * Adds the given permission or permissions set to the user current permissions
* @param $permission * @param $permission
*/ */
public function addPermission($permission) { public function addPermission($permission) {
$tmp = $this->getPermissions(); $tmp = $this->getPermissions();
$tmp = $tmp | $permission; $tmp = $tmp | $permission;
$this->setPermissions($tmp); $this->setPermissions($tmp);
} }
/** /**
* Takes the given permission or permissions out from the user * Takes the given permission or permissions out from the user
* @param $permission * @param $permission
*/ */
public function removePermission($permission) { public function removePermission($permission) {
$tmp = $this->getPermissions(); $tmp = $this->getPermissions();
$tmp = $tmp & ~$permission; $tmp = $tmp & ~$permission;
$this->setPermissions($tmp); $this->setPermissions($tmp);
} }
} }