Skip to content

Commit

Permalink
Better way to keep track of whether a user has a dashboard
Browse files Browse the repository at this point in the history
fixes #2769
  • Loading branch information
brandonkelly committed Apr 18, 2018
1 parent 49ce9c9 commit bfcbe29
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 16 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

## Unreleased

### Added
- Added `craft\elements\User::hasDashboard`.

### Changed
- Sections and category groups now ignore posted Template settings for sites that don’t have URI Formats.

### Fixed
- Fixed a bug where users would regain all default Dashboard widgets if all widgets were removed. ([#2769](https://github.com/craftcms/cms/issues/2769))

## 3.0.3.1 - 2018-04-18

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion src/config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
'id' => 'CraftCMS',
'name' => 'Craft CMS',
'version' => '3.0.3.1',
'schemaVersion' => '3.0.90',
'schemaVersion' => '3.0.91',
'minVersionRequired' => '2.6.2788',
'basePath' => dirname(__DIR__), // Defines the @app alias
'runtimePath' => '@storage/runtime', // Defines the @runtime alias
Expand Down
5 changes: 5 additions & 0 deletions src/elements/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,11 @@ public static function authData(string $authKey)
*/
public $lockoutDate;

/**
* @var bool Whether the user has a dashboard
*/
public $hasDashboard = false;

/**
* @var bool Password reset required
*/
Expand Down
1 change: 1 addition & 0 deletions src/elements/db/UserQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ protected function beforePrepare(): bool
'users.suspended',
'users.lastLoginDate',
'users.lockoutDate',
'users.hasDashboard',
]);

// TODO: remove after next breakpoint
Expand Down
1 change: 1 addition & 0 deletions src/migrations/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,7 @@ public function createTables()
'invalidLoginCount' => $this->tinyInteger()->unsigned(),
'lastInvalidLoginDate' => $this->dateTime(),
'lockoutDate' => $this->dateTime(),
'hasDashboard' => $this->boolean()->notNull()->defaultValue(false),
'verificationCode' => $this->string(),
'verificationCodeIssuedDate' => $this->dateTime(),
'unverifiedEmail' => $this->string(),
Expand Down
45 changes: 45 additions & 0 deletions src/migrations/m180418_205713_widget_cleanup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace craft\migrations;

use Craft;
use craft\db\Migration;
use craft\db\Query;

/**
* m180418_205713_widget_cleanup migration.
*/
class m180418_205713_widget_cleanup extends Migration
{
/**
* @inheritdoc
*/
public function safeUp()
{
$this->addColumn('{{%users}}', 'hasDashboard', $this->boolean()->notNull()->defaultValue(false)->after('lockoutDate'));

$usersWithWidgets = (new Query())
->select(['userId'])
->distinct()
->from(['{{%widgets}}'])
->column();

$this->update('{{%users}}', [
'hasDashboard' => true,
], [
'id' => $usersWithWidgets,
], [], false);

$this->delete('{{%widgets}}', ['enabled' => false]);
$this->dropColumn('{{%widgets}}', 'enabled');
}

/**
* @inheritdoc
*/
public function safeDown()
{
echo "m180418_205713_widget_cleanup cannot be reverted.\n";
return false;
}
}
1 change: 1 addition & 0 deletions src/records/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
* @property int $invalidLoginCount Invalid login count
* @property \DateTime $lastInvalidLoginDate Last invalid login date
* @property \DateTime $lockoutDate Lockout date
* @property bool $hasDashboard Whether the user has a dashboard
* @property string $verificationCode Verification code
* @property \DateTime $verificationCodeIssuedDate Verification code issued date
* @property string $unverifiedEmail Unverified email
Expand Down
1 change: 0 additions & 1 deletion src/records/Widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
* @property int $sortOrder Sort order
* @property int $colspan Colspan
* @property array $settings Settings
* @property bool $enabled Enabled
* @property User $user User
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 3.0
Expand Down
31 changes: 17 additions & 14 deletions src/services/Dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public function getAllWidgets(): array
$widgets = $this->_getUserWidgets();

// If there are no widgets, this is the first time they've hit the dashboard.
if (empty($widgets)) {
if ($widgets === false) {
// Add the defaults and try again
$this->_addDefaultUserWidgets();
$widgets = $this->_getUserWidgets();
Expand All @@ -148,7 +148,6 @@ public function doesUserHaveWidget(string $type): bool
->where([
'userId' => Craft::$app->getUser()->getIdentity()->id,
'type' => $type,
'enabled' => true
])
->exists();
}
Expand Down Expand Up @@ -206,9 +205,6 @@ public function saveWidget(WidgetInterface $widget, bool $runValidation = true):
$widgetRecord->settings = $widget->getSettings();

if ($isNewWidget) {
// Enabled by default.
$widgetRecord->enabled = true;

// Set the sortOrder
$maxSortOrder = (new Query())
->from(['{{%widgets}}'])
Expand Down Expand Up @@ -284,11 +280,8 @@ public function deleteWidget(WidgetInterface $widget): bool
$transaction = Craft::$app->getDb()->beginTransaction();
try {
$widgetRecord = $this->_getUserWidgetRecordById($widget->id);
$widgetRecord->enabled = false;
$widgetRecord->save();

$widgetRecord->delete();
$widget->afterDelete();

$transaction->commit();
} catch (\Throwable $e) {
$transaction->rollBack();
Expand Down Expand Up @@ -379,6 +372,12 @@ private function _addDefaultUserWidgets()
'url' => 'https://craftcms.com/news.rss',
'title' => 'Craft News'
]));

// Update the user record
$user->hasDashboard = true;
Craft::$app->getDb()->createCommand()
->update('{{%users}}', ['hasDashboard' => true], ['id' => $user->id])
->execute();
}

/**
Expand Down Expand Up @@ -422,19 +421,23 @@ private function _noWidgetExists(int $widgetId)
/**
* Returns the widget records for the current user.
*
* @return WidgetInterface[]
* @return WidgetInterface[]|false
* @throws Exception if no user is logged-in
*/
private function _getUserWidgets(): array
private function _getUserWidgets()
{
$userId = Craft::$app->getUser()->getId();
$user = Craft::$app->getUser()->getIdentity();

if (!$userId) {
if (!$user) {
throw new Exception('No logged-in user');
}

if (!$user->hasDashboard) {
return false;
}

$results = $this->_createWidgetsQuery()
->where(['userId' => $userId, 'enabled' => true])
->where(['userId' => $user->id])
->orderBy(['sortOrder' => SORT_ASC])
->all();

Expand Down

0 comments on commit bfcbe29

Please sign in to comment.