Skip to content

Commit

Permalink
feat: services trait + vite 5
Browse files Browse the repository at this point in the history
  • Loading branch information
michtio committed Jan 7, 2025
1 parent 2bb565a commit 30f1f7d
Show file tree
Hide file tree
Showing 24 changed files with 5,450 additions and 5,894 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Release Notes for Password Policy

## 5.0.3 - unreleased
### Fixed
- Fixed a bug that could occur if the max length wasn't set, passwords always said "could not contain more than 0 characters".
- Removed the "playground" from the settings to test the strength indicator, this was only meant for development.
- Fixed an issue where the pwned option would always return that the password was compromised.

## 5.0.2.1 - 2024-12-19
### Fixed
- Fixed `Failed to instantiate component or class` on the assetbundle [Thanks niektenhoopen](https://github.com/craftpulse/craft-password-policy/pull/33)
Expand Down
2,500 changes: 1,614 additions & 886 deletions buildchain/package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions buildchain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
"stylelint-config-recommended-vue": "^1.0.0",
"stylelint-config-standard-scss": "^3.0.0",
"tailwindcss": "^3.0.0",
"vite": "^4.0.0",
"vite": "^5.0.0",
"vite-plugin-compression": "^0.5.0",
"vite-plugin-eslint": "^1.8.0",
"vite-plugin-restart": "^0.3.0",
"vite-plugin-stylelint": "^4.1.0",
"vite-plugin-restart": "^0.4.0",
"vite-plugin-stylelint": "^5.2.1",
"vue-eslint-parser": "^9.0.0"
},
"scripts": {
Expand Down
16 changes: 7 additions & 9 deletions buildchain/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import {defineConfig} from 'vite';
import {visualizer} from 'rollup-plugin-visualizer';
import viteEslintPlugin from 'vite-plugin-eslint';
import viteCompressionPlugin from 'vite-plugin-compression';
import viteRestartPlugin from 'vite-plugin-restart';
import viteStylelintPlugin from 'vite-plugin-stylelint';
import viteVuePlugin from '@vitejs/plugin-vue'
import {defineConfig} from 'vite'
import {visualizer} from 'rollup-plugin-visualizer'
import viteEslintPlugin from 'vite-plugin-eslint'
import viteCompressionPlugin from 'vite-plugin-compression'
import viteRestartPlugin from 'vite-plugin-restart'
import viteStylelintPlugin from 'vite-plugin-stylelint'
import * as path from 'path';

// https://vitejs.dev/config/
export default defineConfig(({command}) => ({
base: command === 'serve' ? '' : '/dist/',
build: {
emptyOutDir: true,
manifest: true,
manifest: 'manifest.json',
outDir: '../src/web/assets/dist',
rollupOptions: {
input: {
Expand All @@ -29,7 +28,6 @@ export default defineConfig(({command}) => ({
'../src/templates/**/*',
],
}),
viteVuePlugin(),
viteCompressionPlugin({
filter: /\.(js|mjs|json|css|map)$/i
}),
Expand Down
Binary file removed resources/img/screenshot.png
Binary file not shown.
Binary file removed resources/img/screenshot2.png
Binary file not shown.
Binary file removed resources/img/screenshot3.png
Binary file not shown.
56 changes: 24 additions & 32 deletions src/PasswordPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
use craftpulse\passwordpolicy\assetbundles\passwordpolicy\PasswordPolicyAsset;
use craftpulse\passwordpolicy\models\SettingsModel;
use craftpulse\passwordpolicy\rules\UserRules;
use craftpulse\passwordpolicy\services\PasswordService;
use craftpulse\passwordpolicy\services\RetentionService;
use craftpulse\passwordpolicy\services\ServicesTrait;
use craftpulse\passwordpolicy\utilities\RetentionUtility;
use craftpulse\passwordpolicy\variables\PasswordPolicyVariable;

Expand All @@ -43,6 +42,7 @@
use nystudio107\pluginvite\services\ViteService;
use Psr\Log\LogLevel;
use Throwable;
use Yii;
use yii\base\Event;
use yii\base\InvalidRouteException;
use yii\log\Dispatcher;
Expand All @@ -65,6 +65,8 @@ class PasswordPolicy extends Plugin
// Traits
// =========================================================================

use ServicesTrait;

// Static Properties
// =========================================================================
/**
Expand Down Expand Up @@ -99,31 +101,6 @@ class PasswordPolicy extends Plugin
*/
public static ?SettingsModel $settings = null;

/**
* @inheritdoc
*/
public static function config(): array
{
return [
'components' => [
'passwords' => PasswordService::class,
'retention' => RetentionService::class,
// Register the vite service
// @TODO devServerPublic / devServerInternal / serverPublic would benefit of `.env` vars for local dev
'vite' => [
'class' => VitePluginService::class,
'assetClass' => PasswordPolicyAsset::class,
'useDevServer' => true,
'devServerPublic' => 'http://localhost:3005',
'serverPublic' => 'http://localhost:8000',
'errorEntry' => 'src/js/PasswordPolicy.js',
'devServerInternal' => 'http://craft-password-policy-v5-buildchain-dev:3005',
'checkDevServer' => true,
],
],
];
}

public function init(): void
{
parent::init();
Expand All @@ -137,12 +114,14 @@ public function init(): void
$this->controllerNamespace = 'craftpulse\passwordpolicy\console\controllers';
}

// Install our global evnet handlers
// Install our global event handlers
$this->installEventHandlers();
$this->installCpEventHandlers();

// Register control panel events
if (Craft::$app->getRequest()->getIsCpRequest()) {
$this->registerCpUrlRules();
$this->installCpEventHandlers();
}

// Log that the plugin has loaded
Expand Down Expand Up @@ -285,6 +264,15 @@ static function(DefineRulesEvent $event) {
}
);

$this->registerUserPermissions();
$this->registerUtilities();
}

/**
* @return void
*/
protected function installCpEventHandlers(): void
{
// Load asset before page template is rendered
Event::on(
View::class,
Expand All @@ -296,13 +284,17 @@ function(TemplateEvent $event) {
// Register Asset Bundle
$view->registerAssetBundle(PasswordPolicyAsset::class);

// Load Asset
//$tagOptions = [
// 'depends' => [
// 'craftpulse\\passwordpolicy\\assetbundles\\passwordpolicy\\PasswordPolicyAsset'
// ],
//];
$manifestPath = '@craftpulse/passwordpolicy/web/assets/dist/';
$this->vite->manifestPath = rtrim(Yii::getAlias($manifestPath), '/\\');
//$this->vite->manifestPath = $manifestPath;
$this->vite->register('src/js/indicator.ts', false);
}
);

$this->registerUserPermissions();
$this->registerUtilities();
}

// Private Methods
Expand Down
2 changes: 1 addition & 1 deletion src/assetbundles/passwordpolicy/PasswordPolicyAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class PasswordPolicyAsset extends AssetBundle
*/
public function init(): void
{
$this->sourcePath = '@craftpulse/passwordpolicy/web/assets/dist';
$this->sourcePath = '@craftpulse/passwordpolicy/web/assets/dist/';
$this->depends = [
CpAsset::class,
];
Expand Down
25 changes: 14 additions & 11 deletions src/rules/UserRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,6 @@ public static function defineRules(): array
),
];
$rules[] =
[
['password', 'newPassword'],
'string',
'max' => $settings->maxLength,
'tooLong' => Craft::t(
'password-policy',
Craft::t('password-policy','Password can maximum contain {max} characters.'),
['max' => $settings->maxLength]
),
];
$rules[] =
[
['password', 'newPassword'],
'match',
Expand All @@ -61,6 +50,20 @@ public static function defineRules(): array
) . PasswordPolicy::$plugin->passwords->generateMessage(),

Check failure on line 50 in src/rules/UserRules.php

View workflow job for this annotation

GitHub Actions / PHPStan

Call to method generateMessage() on an unknown class craftpulse\passwordpolicy\PasswordService.
];

if ($settings->maxLength > $settings->minLength) {
$rules[] =
[
['password', 'newPassword'],
'string',
'max' => $settings->maxLength,
'tooLong' => Craft::t(
'password-policy',
Craft::t('password-policy','Password can maximum contain {max} characters.'),
['max' => $settings->maxLength]
),
];
}

if ($settings->pwned) {
$rules[] = [['password', 'newPassword'], PwnedValidator::class];
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/PasswordService.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function generateMessage(): string
*/
public function pwned(string $password): ?bool
{
$hash = strtoupper(sha1('password'));
$hash = strtoupper(sha1($password));
$prefix = substr($hash, 0, 5);
$suffix = substr($hash, 5);

Expand Down
76 changes: 76 additions & 0 deletions src/services/ServicesTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace craftpulse\passwordpolicy\services;

use craftpulse\passwordpolicy\assetbundles\passwordpolicy\PasswordPolicyAsset;
use nystudio107\pluginvite\services\VitePluginService;
use yii\base\InvalidConfigException;

/**
* @author craftpulse
* @package Password Policy
* @since 5.0.3
*
* @property Passwords $passwords
* @property Retention $retention
* @property VitePluginService $vite
*/
trait ServicesTrait
{
public static function config(): array
{
return [
'components' => [
'passwords' => PasswordService::class,
'retention' => RetentionService::class,
// Register the vite service
// @TODO devServerPublic / devServerInternal / serverPublic would benefit of `.env` vars for local dev
'vite' => [
'assetClass' => PasswordPolicyAsset::class,
'checkDevServer' => true,
'class' => VitePluginService::class,
'devServerInternal' => 'http://craft-password-policy-v5-buildchain-dev:3005',
'devServerPublic' => 'http://localhost:3005',
'errorEntry' => 'src/js/indicator.ts',
'useDevServer' => true,
],
],
];
}

// Public Methods
// =========================================================================

/**
* Returns the passwords service
*
* @return Events The events service
* @throws InvalidConfigException
*/
public function getPasswords(): Events
{
return $this->get('passwords');
}

/**
* Returns the retention service
*
* @return Redirects The redirects service
* @throws InvalidConfigException
*/
public function getRetention(): Redirects
{
return $this->get('retention');
}

/**
* Returns the vite service
*
* @return VitePluginService The vite service
* @throws InvalidConfigException
*/
public function getVite(): VitePluginService
{
return $this->get('vite');
}
}
16 changes: 0 additions & 16 deletions src/templates/_settings.twig
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
features: { label: 'Configuration'|t('password-policy'), url: '#features' },
rules: { label: 'Password Rules'|t('password-policy'), url: '#rules' },
retention: { label: 'Password Retention Features'|t('password-policy'), url: '#retention' },
playground: { label: 'Playground'|t('password-policy'), url: '#playground' },
} %}

{% block actionButton %}
Expand Down Expand Up @@ -136,19 +135,4 @@
id: 'expiryInput',
}, expiryInput) }}
</div>

<div id="playground" class="hidden flex-fields">
<div>
<div id="newPassword-field" class="field" data-attribute="newPassword">
<div class="heading">
<label id="newPassword-label" for="newPassword">New Password</label>
</div>
<div class="input ltr">
<div class="passwordwrapper">
<input type="password" id="newPassword" class="password text fullwidth" name="newPassword" autocomplete="new-password" dir="ltr" aria-labelledby="newPassword-label">
</div>
</div>
</div>
</div>
</div>
{% endblock %}
1 change: 0 additions & 1 deletion src/web/assets/dist/assets/indicator-b0f69606.css

This file was deleted.

1 change: 1 addition & 0 deletions src/web/assets/dist/assets/strengthIndicator-BJVqszZH.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/web/assets/dist/assets/strengthIndicator-DbbUdF_w.js

Large diffs are not rendered by default.

Binary file not shown.

Large diffs are not rendered by default.

Binary file not shown.
6 changes: 0 additions & 6 deletions src/web/assets/dist/assets/strengthIndicator-d691cd8c.js

This file was deleted.

Binary file not shown.
Binary file not shown.
15 changes: 6 additions & 9 deletions src/web/assets/dist/manifest.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
{
"src/js/indicator.css": {
"file": "assets/indicator-b0f69606.css",
"src": "src/js/indicator.css"
},
"src/js/indicator.ts": {
"css": [
"assets/indicator-b0f69606.css"
],
"file": "assets/strengthIndicator-d691cd8c.js",
"file": "assets/strengthIndicator-DbbUdF_w.js",
"name": "strengthIndicator",
"src": "src/js/indicator.ts",
"isEntry": true,
"src": "src/js/indicator.ts"
"css": [
"assets/strengthIndicator-BJVqszZH.css"
]
}
}
Loading

0 comments on commit 30f1f7d

Please sign in to comment.