This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Added more preferences for the user.

  • Loading branch information...
mtancoigne committed Aug 23, 2016
2 parents 0e30502 + 3bf8466 commit 818bdba947bc83e058513e19d06fe65506c32082
Showing with 418 additions and 244 deletions.
  1. +153 −101 clover.xml
  2. +27 −0 config/Migrations/20160823211238_ChangedUserPrefs.php
  3. BIN config/Migrations/schema-dump-default.lock
  4. +2 −3 config/Seeds/UsersSeed.php
  5. +6 −1 config/siteconfig.php
  6. +26 −0 src/Controller/AppController.php
  7. +64 −1 src/Controller/User/UsersController.php
  8. +5 −2 src/Controller/UsersController.php
  9. +0 −1 src/Model/Entity/User.php
  10. +0 −5 src/Model/Table/UsersTable.php
  11. +2 −2 src/Template/User/Files/add.ctp
  12. +1 −1 src/Template/User/Files/edit.ctp
  13. +2 −2 src/Template/User/Notes/add.ctp
  14. +2 −2 src/Template/User/Posts/add.ctp
  15. +2 −2 src/Template/User/Projects/add.ctp
  16. +88 −76 src/Template/User/Users/edit.ctp
  17. +1 −1 src/View/AppView.php
  18. +4 −9 tests/Fixture/UsersFixture.php
  19. +1 −1 tests/TestCase/Controller/Admin/ActsControllerTest.php
  20. +4 −4 tests/TestCase/Controller/Admin/AdminAppControllerTest.php
  21. +1 −1 tests/TestCase/Controller/Admin/DashboardControllerTest.php
  22. +1 −1 tests/TestCase/Controller/Admin/FilesControllerTest.php
  23. +1 −1 tests/TestCase/Controller/Admin/LanguagesControllerTest.php
  24. +1 −1 tests/TestCase/Controller/Admin/LicensesControllerTest.php
  25. +1 −1 tests/TestCase/Controller/Admin/MaintenanceControllerTest.php
  26. +1 −1 tests/TestCase/Controller/Admin/NotesControllerTest.php
  27. +1 −1 tests/TestCase/Controller/Admin/PostsControllerTest.php
  28. +1 −1 tests/TestCase/Controller/Admin/ProjectsControllerTest.php
  29. +1 −1 tests/TestCase/Controller/Admin/ReportsControllerTest.php
  30. +1 −1 tests/TestCase/Controller/Admin/TagsControllerTest.php
  31. +1 −1 tests/TestCase/Controller/Admin/TeamsControllerTest.php
  32. +1 −1 tests/TestCase/Controller/Admin/UsersControllerTest.php
  33. +1 −1 tests/TestCase/Controller/User/DashboardControllerTest.php
  34. +1 −2 tests/TestCase/Controller/User/FilesControllerTest.php
  35. +1 −1 tests/TestCase/Controller/User/NotesControllerTest.php
  36. +1 −1 tests/TestCase/Controller/User/PostsControllerTest.php
  37. +1 −1 tests/TestCase/Controller/User/ProjectsControllerTest.php
  38. +1 −1 tests/TestCase/Controller/User/TeamsControllerTest.php
  39. +5 −4 tests/TestCase/Controller/User/UserAppControllerTest.php
  40. +5 −7 tests/TestCase/Controller/User/UsersControllerTest.php

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -0,0 +1,27 @@
<?php
use Migrations\AbstractMigration;
class ChangedUserPrefs extends AbstractMigration
{
public function up()
{
$this->table('users')
->removeColumn('see_nsfw')
->update();
}
public function down()
{
$this->table('users')
->addColumn('see_nsfw', 'boolean', [
'default' => 0,
'length' => null,
'null' => false,
])
->update();
}
}
Binary file not shown.
@@ -1,7 +1,6 @@
<?php
use Cake\Auth\DefaultPasswordHasher;
use Cake\I18n\FrozenTime;
use Cake\Utility\Text;
use Migrations\AbstractSeed;
@@ -23,7 +22,7 @@ class UsersSeed extends AbstractSeed
*/
public function run()
{
$time = FrozenTime::now();
$time = Cake\I18n\Time::now();
$data = [
[
'id' => Text::uuid(),
@@ -36,7 +35,7 @@ public function run()
'role' => 'admin',
'see_nsfw' => '1',
'status' => '1',
'preferences' => '{}'
'preferences' => json_encode(Cake\Core\Configure::read('cms.defaultUserPreferences'))
]
];
@@ -9,7 +9,12 @@
'isRegistrationOpen' => true,
'defaultRole' => 'author',
'defaultUserStatus' => 0,
'defaultUserPreferences' => [],
'defaultUserPreferences' => [
'showNSFW' => '0',
'defaultSiteLanguage' => '',
'defaultWritingLanguage' => 'eng',
'defaultWritingLicense' => '1',
],
'defaultLockedUser' => false,
'defaultSeeNSFW' => false,
'defaultMinPassLenght' => 8,
@@ -129,6 +129,32 @@ protected function _setSFWState($state = 'hide')
$this->request->session()->write('seeNSFW', ($state === 'show') ? true : false);
}
/**
* Sets values in session with user preferences.
*
*
* @return void
*/
protected function _setUserPreferences()
{
$preferences = json_decode($this->Auth->user('preferences'), true);
$this->_setLanguage($preferences['defaultSiteLanguage']);
$this->_setSFWState(($preferences['showNSFW'] === '1') ? 'show' : 'hide');
$this->request->session()->write('defaultWritingLanguage', $preferences['defaultWritingLanguage']);
$this->request->session()->write('defaultWritingLicense', $preferences['defaultWritingLicense']);
}
/**
* Clear user specific preferences
*
* @return void
*/
protected function _clearUserPreferences()
{
$this->request->session()->delete('defaultWritingLanguage');
$this->request->session()->delete('defaultWritingLicense');
}
/**
* Finds available website translations and write them in session
*
@@ -23,16 +23,79 @@ public function edit()
if ($this->request->is(['patch', 'post', 'put'])) {
$user = $this->Users->patchEntity($user, $this->request->data);
if ($this->Users->save($user)) {
$this->updateAuthUser($user);
$this->Flash->success(__d('elabs', 'Your informations are now up to date.'));
$this->redirect(['action' => 'edit']);
} else {
$this->Flash->error(__d('elabs', 'An error occured. Please try again.'));
}
}
$this->set(compact('user'));
$this->Languages = \Cake\ORM\TableRegistry::get('Languages');
$sLanguages = $this->Languages->find('list', ['fields' => ['id' => 'translation_folder', 'name'], 'conditions' => ['has_site_translation' => true]]);
$wLanguages = $this->Languages->find('list');
$this->Licenses = \Cake\ORM\TableRegistry::get('Licenses');
$licenses = $this->Licenses->find('list');
$this->set(compact('user', 'wLanguages', 'sLanguages', 'licenses'));
$this->set('_serialize', ['user']);
}
/**
* Updates user preferences
*
* @return void Redirects on successful edit
*/
public function updatePrefs()
{
$user = $this->Users->get($this->Auth->user('id'));
if ($this->request->is(['patch', 'post', 'put'])) {
// Preparing data. Keys from site defaults will be used to avoid wrong keys.
$defaults = \Cake\Core\Configure::read('cms.defaultUserPreferences');
$userPrefs = [];
foreach ($defaults as $k => $v) {
$userPrefs[$k] = $this->request->data['preferences'][$k];
}
$user = $this->Users->patchEntity($user, ['preferences' => json_encode($userPrefs)]);
if ($this->Users->save($user)) {
$this->updateAuthUser($user);
$this->Flash->success(__d('elabs', 'Your preferences have been updated.'));
$this->redirect(['action' => 'edit']);
} else {
$this->Flash->error(__d('elabs', 'An error occured. Please try again.'));
}
} else {
die('nope');
}
}
/**
* Update the session of an user with new values
*
* @param \App\Model\Entity\User $user Updated user data
*
* @return void
*/
protected function updateAuthUser(\App\Model\Entity\User $user)
{
$this->Auth->setUser([
'id' => $user->id,
'email' => $user->email,
'username' => $user->username,
'realname' => $user->realname,
'website' => $user->website,
'bio' => $user->bio,
'created' => $user->created,
'modified' => $user->modified,
'role' => $user->role,
'status' => $user->status,
'file_count' => $user->file_count,
'note_count' => $user->note_count,
'post_count' => $user->post_count,
'project_count' => $user->project_count,
'preferences' => $user->preferences
]);
$this->_setUserPreferences($user->preferences);
}
/**
* Update the password in DB
*
@@ -24,7 +24,7 @@ public function beforeFilter(\Cake\Event\Event $event)
{
parent::beforeFilter($event);
if (in_array($this->request->action, ['register', 'login']) && !empty($this->Auth->user('id'))) {
throw new ForbiddenException(__d('elabs', 'You are already registered'));
throw new ForbiddenException(__d('elabs', 'You are already logged in'));
}
if (!Configure::read('cms.isRegistrationOpen') && $this->request->action === 'register') {
throw new ForbiddenException(__d('elabs', 'Registrations are closed for now... Come back later...'), 403);
@@ -163,7 +163,8 @@ public function login()
$user = $this->Auth->identify();
if ($user) {
$this->Auth->setUser($user);
$this->request->session()->write('seeNSFW', $this->Auth->User('seeNSFW'));
// Overwrite preferences:
$this->_setUserPreferences();
$this->redirect($this->Auth->redirectUrl());
} else {
$this->Flash->error(__d('elabs', 'Invalid username or password, try again. Is your accound active ?'));
@@ -178,6 +179,8 @@ public function login()
*/
public function logout()
{
// Remove user preferences
$this->_clearUserPreferences();
$this->Flash->success(__d('elabs', 'You are logged out. See you later !'));
$this->redirect($this->Auth->logout());
}
@@ -18,7 +18,6 @@
* @property \Cake\I18n\Time $created
* @property \Cake\I18n\Time $modified
* @property string $role
* @property bool $see_nsfw
* @property int $status
* @property int $file_count
* @property int $note_count
@@ -133,11 +133,6 @@ public function validationDefault(Validator $validator)
->add('role', 'inlist', ['rule' => ['inList', ['admin', 'author', 'user']],
'message' => 'Please enter a valid role']);
$validator
->boolean('see_nsfw')
->requirePresence('see_nsfw', 'create')
->notEmpty('see_nsfw');
$validator
->integer('status')
->requirePresence('status', 'create')
@@ -33,10 +33,10 @@ $this->CodeMirror->add('descArea');
?>
<div class="row">
<div class="col-sm-6">
<?php echo $this->Form->input('license_id', ['options' => $licenses]); ?>
<?php echo $this->Form->input('license_id', ['options' => $licenses, 'default' => $this->request->session()->read('defaultWritingLicense')]); ?>
</div>
<div class="col-sm-6">
<?php echo $this->Form->input('language_id', ['options' => $languages, 'default' => 'fra']); ?>
<?php echo $this->Form->input('language_id', ['options' => $languages, 'default' => $this->request->session()->read('defaultWritingLanguage')]); ?>
</div>
</div>
<div class="row">
@@ -42,7 +42,7 @@ $this->CodeMirror->add('descArea');
<?php echo $this->Form->input('license_id', ['options' => $licenses]); ?>
</div>
<div class="col-sm-6">
<?php echo $this->Form->input('language_id', ['options' => $languages, 'default' => 'fra']); ?>
<?php echo $this->Form->input('language_id', ['options' => $languages]); ?>
</div>
</div>
<div class="row">
@@ -33,10 +33,10 @@ $this->CodeMirror->add('noteArea', [], ['%s.setSize(null, "150")']);
?>
<div class="row">
<div class="col-sm-6">
<?php echo $this->Form->input('license_id', ['options' => $licenses]); ?>
<?php echo $this->Form->input('license_id', ['options' => $licenses, 'default' => $this->request->session()->read('defaultWritingLicense')]); ?>
</div>
<div class="col-sm-6">
<?php echo $this->Form->input('language_id', ['options' => $languages]); ?>
<?php echo $this->Form->input('language_id', ['options' => $languages, 'default' => $this->request->session()->read('defaultWritingLanguage')]); ?>
</div>
</div>
<div class="row">
@@ -36,10 +36,10 @@ $this->CodeMirror->add('textArea');
?>
<div class="row">
<div class="col-sm-6">
<?php echo $this->Form->input('license_id', ['options' => $licenses]); ?>
<?php echo $this->Form->input('license_id', ['options' => $licenses, 'default' => $this->request->session()->read('defaultWritingLicense')]); ?>
</div>
<div class="col-sm-6">
<?php echo $this->Form->input('language_id', ['options' => $languages]); ?>
<?php echo $this->Form->input('language_id', ['options' => $languages, 'default' => $this->request->session()->read('defaultWritingLanguage')]); ?>
</div>
</div>
<div class="row">
@@ -37,10 +37,10 @@ $this->CodeMirror->add('descriptionArea');
?>
<div class="row">
<div class="col-sm-6">
<?php echo $this->Form->input('license_id', ['options' => $licenses]); ?>
<?php echo $this->Form->input('license_id', ['options' => $licenses, 'default' => $this->request->session()->read('defaultWritingLicense')]); ?>
</div>
<div class="col-sm-6">
<?php echo $this->Form->input('language_id', ['options' => $languages]); ?>
<?php echo $this->Form->input('language_id', ['options' => $languages, 'default' => $this->request->session()->read('defaultWritingLanguage')]); ?>
</div>
</div>
<div class="row">
Oops, something went wrong.

0 comments on commit 818bdba

Please sign in to comment.