Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code standard #89

Merged
merged 2 commits into from
Aug 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions appinfo/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,8 @@
// only load notification JS code in the logged in layout page (not public links not login page)
$request = \OC::$server->getRequest();
if (\OC::$server->getUserSession() !== null && \OC::$server->getUserSession()->getUser() !== null
&& substr($request->getScriptName(), 0 - strlen('/index.php')) === '/index.php'
&& substr($request->getPathInfo(), 0, strlen('/s/')) !== '/s/'
&& substr($request->getPathInfo(), 0, strlen('/login')) !== '/login') {

&& \substr($request->getScriptName(), 0 - \strlen('/index.php')) === '/index.php'
&& \substr($request->getPathInfo(), 0, \strlen('/s/')) !== '/s/'
&& \substr($request->getPathInfo(), 0, \strlen('/login')) !== '/login') {
\OCP\Util::addScript('password_policy', 'notification');
}
3 changes: 1 addition & 2 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
use OCA\PasswordPolicy\Notifier;

class Application extends App {

public function __construct (array $urlParams = []) {
public function __construct(array $urlParams = []) {
parent::__construct('password_policy', $urlParams);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Authentication/AccountModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function check(IUser $user) {
if ($forcePasswordChange) {
$redirectUrl = $this->request->getRequestUri();
$response = new RedirectResponse(
$this->urlGenerator->linkToRoute( 'password_policy.password.show', [
$this->urlGenerator->linkToRoute('password_policy.password.show', [
'redirect_url' => $redirectUrl
])
);
Expand Down
4 changes: 1 addition & 3 deletions lib/Command/ExpirePassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
use Symfony\Component\Console\Output\OutputInterface;
use OCP\AppFramework\Utility\ITimeFactory;


class ExpirePassword extends Command {
/**
* Catchall for general errors
Expand Down Expand Up @@ -64,7 +63,6 @@ class ExpirePassword extends Command {
/** @var OldPasswordMapper */
private $mapper;


/**
* ExpirePassword constructor.
*
Expand Down Expand Up @@ -200,7 +198,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
* @param $inputExpireDate
* @param IUser $user
*/
protected function setPasswordExpiry(\DateTime $expireDate, \DateTime $oldDate, \DateTimeZone $timeZone, $inputExpireDate , IUser $user) {
protected function setPasswordExpiry(\DateTime $expireDate, \DateTime $oldDate, \DateTimeZone $timeZone, $inputExpireDate, IUser $user) {
$expireDate->setTimezone($timeZone);
$expireDate->setTimestamp($this->timeFactory->getTime());
$expireDate->modify($inputExpireDate);
Expand Down
2 changes: 1 addition & 1 deletion lib/Controller/PasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public function show($redirect_url) {
*/
public function update($current_password, $new_password, $confirm_password, $redirect_url) {
$user = $this->userSession->getUser();
if(($user !== null) && !$this->userManager->checkPassword($user->getUID(), $current_password)) {
if (($user !== null) && !$this->userManager->checkPassword($user->getUID(), $current_password)) {
return $this->createPasswordTemplateResponse(
$redirect_url,
$this->l10n->t('Old password is wrong.')
Expand Down
2 changes: 1 addition & 1 deletion lib/Controller/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function getPriority() {

public function getPanel() {
$template = new Template('password_policy', 'admin');
foreach(self::DEFAULTS as $key => $default) {
foreach (self::DEFAULTS as $key => $default) {
$value = $this->config->getAppValue('password_policy', $key, $default);
if (isset(self::CONVERSIONS[$key]['out'])) {
$convertFuncName = self::CONVERSIONS[$key]['out'];
Expand Down
4 changes: 2 additions & 2 deletions lib/Db/OldPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
namespace OCA\PasswordPolicy\Db;

use OCP\AppFramework\Db\Entity;

/**
* @method int getId()
* @method void setId(\int $id)
Expand All @@ -33,7 +34,6 @@
* @method void setChangeTime(\int $changeTime)
*/
class OldPassword extends Entity {

const EXPIRED = 'expired';

/** @var string $uid */
Expand All @@ -48,4 +48,4 @@ class OldPassword extends Entity {
public function getPassword() {
return $this->password;
}
}
}
1 change: 0 additions & 1 deletion lib/Db/OldPasswordMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
use OCP\IDBConnection;

class OldPasswordMapper extends Mapper {

public function __construct(IDBConnection $db) {
parent::__construct($db, 'user_password_history');
}
Expand Down
6 changes: 3 additions & 3 deletions lib/HooksHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public function updateLinkExpiry($params) {

if ($days !== null) {
$date = new \DateTime();
$date->setTime(0,0,0);
$date->setTime(0, 0, 0);
$date->add(new \DateInterval('P'.$days.'D'));

if ($params['expirationDate'] === null) {
Expand Down Expand Up @@ -272,7 +272,7 @@ protected function forcePasswordChange($firstLogin = null, IUser $user = null) {
*/
private function getUser(GenericEvent $event) {
$user = $event->getArgument('user');
if(!$user instanceof IUser) {
if (!$user instanceof IUser) {
throw new \UnexpectedValueException("'$user' is not an instance of IUser.");
}
return $user;
Expand Down Expand Up @@ -319,7 +319,7 @@ public function checkAdminRequestedPasswordChange(GenericEvent $event) {
public function checkForcePasswordChangeOnFirstLogin(GenericEvent $event) {
$this->fixDI();
$user = $event->getSubject();
if(!$user instanceof IUser) {
if (!$user instanceof IUser) {
throw new \UnexpectedValueException("'$user' is not an instance of IUser.");
}
// If option is enabled in security settings check first login
Expand Down
2 changes: 0 additions & 2 deletions lib/Rules/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,4 @@ class Base {
public function __construct(IL10N $l10n) {
$this->l10n = $l10n;
}

}

1 change: 0 additions & 1 deletion lib/Rules/Length.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,4 @@ public function verify($password, $val) {
$this->l10n->t('The password is too short. At least %d characters are required.', [$val]));
}
}

}
1 change: 0 additions & 1 deletion lib/Rules/Lowercase.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,4 @@ private function countLowercase($s) {
}
return $count;
}

}
4 changes: 2 additions & 2 deletions lib/Rules/Numbers.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function verify($password, $val) {
}
}

private function countDigits( $str ) {
return \preg_match_all( '/[0-9]/', $str );
private function countDigits($str) {
return \preg_match_all('/[0-9]/', $str);
}
}
4 changes: 2 additions & 2 deletions lib/Rules/PasswordHistory.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ public function __construct(
* @throws PolicyException
*/
public function verify($password, $val, $uid) {
if(empty($uid)) {
if (empty($uid)) {
return;
}
$oldPasswords = $this->mapper->getOldPasswords(
$uid,
$val
);
foreach($oldPasswords as $oldPassword) {
foreach ($oldPasswords as $oldPassword) {
if ($this->hasher->verify($password, $oldPassword->getPassword())) {
throw new PolicyException(
$this->l10n->t('The password must be different than your previous %d passwords.', [$val])
Expand Down
8 changes: 3 additions & 5 deletions lib/Rules/Special.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function verify($password, $val, $allowedSpecialChars) {
$special = $this->stripAlphaNumeric($password);
if (!empty($allowedSpecialChars) && !empty($special)) {
$allowedSpecialCharsAsArray = \str_split($allowedSpecialChars);
$s = \array_filter(\str_split($special), function($char) use ($allowedSpecialCharsAsArray){
$s = \array_filter(\str_split($special), function ($char) use ($allowedSpecialCharsAsArray) {
return !\in_array($char, $allowedSpecialCharsAsArray, true);
});
if (\count($s) > 0) {
Expand Down Expand Up @@ -63,9 +63,7 @@ public function verify($password, $val, $allowedSpecialChars) {
}
}

private function stripAlphaNumeric( $string ) {
return \preg_replace( '/[a-z0-9]/i', '', $string );
private function stripAlphaNumeric($string) {
return \preg_replace('/[a-z0-9]/i', '', $string);
}

}

1 change: 0 additions & 1 deletion lib/Rules/Uppercase.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,4 @@ private function countUppercase($s) {
}
return $count;
}

}
4 changes: 3 additions & 1 deletion templates/password.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@
?>
</div>
</h1>
<?php if (isset($_['error'])) { ?><div id="error" class="warning"><?php p($_['error']) ?></div> <?php } ?>
<?php if (isset($_['error'])) {
?><div id="error" class="warning"><?php p($_['error']) ?></div> <?php
} ?>
<input type="hidden" name="redirect_url" value="<?php p($_['redirect_url']) ?>" />
<input type="hidden" name="app" value="oca-password-policy" />
<input type="hidden" name="requesttoken" value="<?php p($_['requesttoken']) ?>" id="requesttoken">
Expand Down
2 changes: 1 addition & 1 deletion tests/Command/ExpirePasswordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ public function testExpirePassword(
$oldPassword = null;
$this->mapper->expects($this->once())
->method('insert')
->will($this->returnCallback(function($obj) use (&$oldPassword) {
->will($this->returnCallback(function ($obj) use (&$oldPassword) {
$oldPassword = $obj;
}));

Expand Down
4 changes: 2 additions & 2 deletions tests/Controller/PasswordControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ public function setUp() {
$this->l10n = $this->createMock(IL10N::class);
$this->l10n
->method('t')
->will($this->returnCallback(function($text, $parameters = array()) {
return vsprintf($text, $parameters);
->will($this->returnCallback(function ($text, $parameters = []) {
return \vsprintf($text, $parameters);
}));
$this->c = $this->getMockBuilder(PasswordController::class)
->setConstructorArgs([
Expand Down
3 changes: 1 addition & 2 deletions tests/EngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
use Test\TestCase;

class EngineTest extends TestCase {

private $defaults = [
'spv_min_chars_checked' => false,
'spv_min_chars_value' => 8,
Expand Down Expand Up @@ -62,7 +61,7 @@ protected function setUp() {
$this->l10n = $this->createMock(IL10N::class);
$this->l10n
->method('t')
->will($this->returnCallback(function($text, $parameters = []) {
->will($this->returnCallback(function ($text, $parameters = []) {
return \vsprintf($text, $parameters);
}));

Expand Down
33 changes: 16 additions & 17 deletions tests/HooksHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ protected function setUp() {
$this->l10n = $this->createMock(IL10N::class);
$this->l10n
->method('t')
->will($this->returnCallback(function($text, $parameters = []) {
->will($this->returnCallback(function ($text, $parameters = []) {
return \vsprintf($text, $parameters);
}));
$this->passwordExpiredRule = $this->createMock(PasswordExpired::class);
Expand All @@ -98,31 +98,31 @@ protected function setUp() {
);

$this->manager->method('createNotification')
->will($this->returnCallback(function() {
->will($this->returnCallback(function () {
$holder = [];
$mock = $this->createMock(INotification::class);
$mock->method('setApp')->will($this->returnCallback(function($app) use (&$holder, $mock) {
$mock->method('setApp')->will($this->returnCallback(function ($app) use (&$holder, $mock) {
$holder['app'] = $app;
return $mock;
}));
$mock->method('setUser')->will($this->returnCallback(function($user) use (&$holder, $mock) {
$mock->method('setUser')->will($this->returnCallback(function ($user) use (&$holder, $mock) {
$holder['user'] = $user;
return $mock;
}));
$mock->method('setObject')->will($this->returnCallback(function($obj, $id) use (&$holder, $mock) {
$mock->method('setObject')->will($this->returnCallback(function ($obj, $id) use (&$holder, $mock) {
$holder['object'] = [$obj, $id];
return $mock;
}));
$mock->method('getApp')->will($this->returnCallback(function() use (&$holder) {
$mock->method('getApp')->will($this->returnCallback(function () use (&$holder) {
return $holder['app'];
}));
$mock->method('getUser')->will($this->returnCallback(function() use (&$holder) {
$mock->method('getUser')->will($this->returnCallback(function () use (&$holder) {
return $holder['user'];
}));
$mock->method('getObjectType')->will($this->returnCallback(function() use (&$holder) {
$mock->method('getObjectType')->will($this->returnCallback(function () use (&$holder) {
return $holder['object'][0];
}));
$mock->method('getObjectId')->will($this->returnCallback(function() use (&$holder) {
$mock->method('getObjectId')->will($this->returnCallback(function () use (&$holder) {
return $holder['object'][1];
}));
return $mock;
Expand Down Expand Up @@ -176,10 +176,10 @@ public function testVerifyPublicPassword() {

public function updateLinkExpiryProvider() {
$tomorrow = new \DateTime();
$tomorrow->setTime(0,0,0);
$tomorrow->setTime(0, 0, 0);
$tomorrow->add(new \DateInterval('P1D')); // tomorrow
$in5days = new \DateTime();
$in5days->setTime(0,0,0);
$in5days->setTime(0, 0, 0);
$in5days->add(new \DateInterval('P5D')); // in 5 days

$accepted = true; // needs to be reset on every test set
Expand Down Expand Up @@ -209,7 +209,6 @@ public function testUpdateLinkExpiry($params, $expected) {
$params['accepted'] = true; // needs to be reset on every test set
$this->handler->updateLinkExpiry($params);
self::assertSame($expected, $params['accepted']);

}

public function testSaveOldPassword() {
Expand All @@ -228,7 +227,7 @@ public function testSaveOldPassword() {

$this->oldPasswordMapper->expects($this->once())
->method('insert')
->with($this->callback(function(OldPassword $oldPassword){
->with($this->callback(function (OldPassword $oldPassword) {
return $oldPassword->getPassword() === 'somehash' &&
$oldPassword->getUid() === 'testuid' &&
$oldPassword->getChangeTime() === 12345;
Expand Down Expand Up @@ -256,7 +255,7 @@ public function testSavePasswordForCreatedUser() {

$this->oldPasswordMapper->expects($this->once())
->method('insert')
->with($this->callback(function(OldPassword $oldPassword){
->with($this->callback(function (OldPassword $oldPassword) {
return $oldPassword->getPassword() === 'somehash' &&
$oldPassword->getUid() === 'testuid' &&
$oldPassword->getChangeTime() === 12345;
Expand Down Expand Up @@ -285,7 +284,7 @@ public function testSaveOldPasswordClearingNotifications() {

$this->oldPasswordMapper->expects($this->once())
->method('insert')
->with($this->callback(function(OldPassword $oldPassword){
->with($this->callback(function (OldPassword $oldPassword) {
return $oldPassword->getPassword() === 'somehash' &&
$oldPassword->getUid() === 'testuid' &&
$oldPassword->getChangeTime() === 12345;
Expand All @@ -303,15 +302,15 @@ public function testSaveOldPasswordClearingNotifications() {
->method('markProcessed')
->withConsecutive(
[
$this->callback(function($notif) {
$this->callback(function ($notif) {
return $notif->getApp() === 'password_policy' &&
$notif->getUser() === 'testuid' &&
$notif->getObjectType() === 'about_to_expire' &&
$notif->getObjectId() === '222';
})
],
[
$this->callback(function($notif) {
$this->callback(function ($notif) {
return $notif->getApp() === 'password_policy' &&
$notif->getUser() === 'testuid' &&
$notif->getObjectType() === 'expired' &&
Expand Down
Loading