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

er #60

Open
wants to merge 9 commits into
base: 3.0
Choose a base branch
from
Open

er #60

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
Empty file modified .gitignore
100644 → 100755
Empty file.
Empty file modified .travis.yml
100644 → 100755
Empty file.
Empty file modified LICENSE
100644 → 100755
Empty file.
19 changes: 19 additions & 0 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,25 @@ The default values are shown below.
// Override table names, if necessary.
'userTableName' => 'users',
'userCustomFieldsTableName' => 'user_custom_fields',

//Override Column names, if necessary
'userColumns' = array(
'id' => 'id',
'email' => 'email',
'password' => 'password',
'salt' => 'salt',
'roles' => 'roles',
'name' => 'name',
'time_created' => 'time_created',
'username' => 'username',
'isEnabled' => 'isEnabled',
'confirmationToken' => 'confirmationToken',
'timePasswordResetRequested' => 'timePasswordResetRequested',
//Custom Fields
'user_id' => 'user_id',
'attribute' => 'attribute',
'value' => 'value',
),
);

More information
Expand Down
17 changes: 2 additions & 15 deletions composer.json
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,23 +1,10 @@
{
"name": "jasongrimes/silex-simpleuser",
"type": "library",
"description": "A simple database-backed user provider for Silex, with associated services and controllers.",
"keywords": ["silex", "user", "user provider", "simple user provider"],
"homepage": "http://github.com/jasongrimes/silex-simpleuser",
"license": "BSD-2-Clause",
"authors": [
{
"name": "Jason Grimes",
"email": "[email protected]"
}
],
"name": "LIME-/silex-simpleuser",
"require": {
"php": ">=5.3.0",
"silex/silex": "~1.0",
"symfony/security": "~2.3",
"doctrine/dbal": "~2.4",
"jasongrimes/php-paginator": "*",
"symfony/twig-bridge": "~2.3",
"jasongrimes/paginator": "~1.0",
"swiftmailer/swiftmailer": "~5.3"
},
"require-dev": {
Expand Down
Empty file modified phpunit.xml.dist
100644 → 100755
Empty file.
Empty file modified sql/MIGRATION.md
100644 → 100755
Empty file.
Empty file modified sql/mysql.sql
100644 → 100755
Empty file.
Empty file modified sql/sqlite.sql
100644 → 100755
Empty file.
Empty file modified src/SimpleUser/EditUserVoter.php
100644 → 100755
Empty file.
Empty file modified src/SimpleUser/Mailer.php
100644 → 100755
Empty file.
Empty file modified src/SimpleUser/Migration/MigrateV1ToV2.php
100644 → 100755
Empty file.
Empty file modified src/SimpleUser/TokenGenerator.php
100644 → 100755
Empty file.
Empty file modified src/SimpleUser/User.php
100644 → 100755
Empty file.
Empty file modified src/SimpleUser/UserController.php
100644 → 100755
Empty file.
Empty file modified src/SimpleUser/UserEvent.php
100644 → 100755
Empty file.
Empty file modified src/SimpleUser/UserEvents.php
100644 → 100755
Empty file.
107 changes: 76 additions & 31 deletions src/SimpleUser/UserManager.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,25 @@ class UserManager implements UserProviderInterface
/** @var string */
protected $userCustomFieldsTableName = 'user_custom_fields';

/** @var array */
protected $userColumns = array(
'id' => 'id',
'email' => 'email',
'password' => 'password',
'salt' => 'salt',
'roles' => 'roles',
'name' => 'name',
'time_created' => 'time_created',
'username' => 'username',
'isEnabled' => 'isEnabled',
'confirmationToken' => 'confirmationToken',
'timePasswordResetRequested' => 'timePasswordResetRequested',
//Custom Fields
'user_id' => 'user_id',
'attribute' => 'attribute',
'value' => 'value',
);

/**
* Constructor.
*
Expand Down Expand Up @@ -69,15 +88,15 @@ public function __construct(Connection $conn, Application $app)
public function loadUserByUsername($username)
{
if (strpos($username, '@') !== false) {
$user = $this->findOneBy(array('email' => $username));
$user = $this->findOneBy(array($this->getUserColumns('email') => $username));
if (!$user) {
throw new UsernameNotFoundException(sprintf('Email "%s" does not exist.', $username));
}

return $user;
}

$user = $this->findOneBy(array('username' => $username));
$user = $this->findOneBy(array($this->getUserColumns('username') => $username));
if (!$user) {
throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username));
}
Expand Down Expand Up @@ -130,7 +149,12 @@ protected function hydrateUser(array $data)
{
// Test for new columns added in v2.0.
// If they're missing, throw an exception and explain that migration is needed.
foreach (array('username', 'isEnabled', 'confirmationToken', 'timePasswordResetRequested') as $col) {
foreach (array(
$this->getUserColumns('username'),
$this->getUserColumns('isEnabled'),
$this->getUserColumns('confirmationToken'),
$this->getUserColumns('timePasswordResetRequested')
) as $col) {
if (!array_key_exists($col, $data)) {
throw new \RuntimeException('Internal error: database schema appears out of date. See https://github.com/jasongrimes/silex-simpleuser/blob/master/sql/MIGRATION.md');
}
Expand Down Expand Up @@ -330,7 +354,7 @@ function isLoggedIn()
*/
public function getUser($id)
{
return $this->findOneBy(array('id' => $id));
return $this->findOneBy(array($this->getUserColumns('id') => $id));
}

/**
Expand Down Expand Up @@ -363,8 +387,9 @@ public function findOneBy(array $criteria)
public function findBy(array $criteria = array(), array $options = array())
{
// Check the identity map first.
if (array_key_exists('id', $criteria) && array_key_exists($criteria['id'], $this->identityMap)) {
return array($this->identityMap[$criteria['id']]);
if (array_key_exists($this->getUserColumns('id'), $criteria)
&& array_key_exists($criteria[$this->getUserColumns('id')], $this->identityMap)) {
return array($this->identityMap[$criteria[$this->getUserColumns('id')]]);
}

list ($common_sql, $params) = $this->createCommonFindSql($criteria);
Expand All @@ -384,10 +409,10 @@ public function findBy(array $criteria = array(), array $options = array())

$users = array();
foreach ($data as $userData) {
if (array_key_exists($userData['id'], $this->identityMap)) {
$user = $this->identityMap[$userData['id']];
if (array_key_exists($userData[$this->getUserColumns('id')], $this->identityMap)) {
$user = $this->identityMap[$userData[$this->getUserColumns('id')]];
} else {
$userData['customFields'] = $this->getUserCustomFields($userData['id']);
$userData['customFields'] = $this->getUserCustomFields($userData[$this->getUserColumns('id')]);
$user = $this->hydrateUser($userData);
$this->identityMap[$user->getId()] = $user;
}
Expand All @@ -407,7 +432,7 @@ protected function getUserCustomFields($userId)

$rows = $this->conn->fetchAll('SELECT * FROM ' . $this->conn->quoteIdentifier($this->userCustomFieldsTableName). ' WHERE user_id = ?', array($userId));
foreach ($rows as $row) {
$customFields[$row['attribute']] = $row['value'];
$customFields[$row[$this->getUserColumns('attribute')]] = $row[$this->getUserColumns('value')];
}

return $customFields;
Expand All @@ -431,9 +456,9 @@ protected function createCommonFindSql(array $criteria = array())
$i++;
$alias = 'custom' . $i;
$sql .= 'JOIN ' . $this->conn->quoteIdentifier($this->userCustomFieldsTableName). ' ' . $alias . ' ';
$sql .= 'ON ' . $this->conn->quoteIdentifier($this->userTableName). '.id = ' . $alias . '.user_id ';
$sql .= 'AND ' . $alias . '.attribute = :attribute' . $i . ' ';
$sql .= 'AND ' . $alias . '.value = :value' . $i . ' ';
$sql .= 'ON ' . $this->conn->quoteIdentifier($this->userTableName). '.' . $this->getUserColumns('id') . ' = ' . $alias . '.'. $this->getUserColumns('user_id').' ';
$sql .= 'AND ' . $alias . '.'.$this->getUserColumns('attribute').' = :attribute' . $i . ' ';
$sql .= 'AND ' . $alias . '.'.$this->getUserColumns('value').' = :value' . $i . ' ';
$params['attribute' . $i] = $attribute;
$params['value' . $i] = $value;
}
Expand Down Expand Up @@ -478,7 +503,9 @@ public function insert(User $user)
$this->dispatcher->dispatch(UserEvents::BEFORE_INSERT, new UserEvent($user));

$sql = 'INSERT INTO ' . $this->conn->quoteIdentifier($this->userTableName) . '
(email, password, salt, name, roles, time_created, username, isEnabled, confirmationToken, timePasswordResetRequested)
('.$this->getUserColumns('email').', '.$this->getUserColumns('password').', '.$this->getUserColumns('salt').', '.$this->getUserColumns('name').
', '.$this->getUserColumns('roles').', '.$this->getUserColumns('time_created').', '.$this->getUserColumns('username').', '.$this->getUserColumns('isEnabled').
', '.$this->getUserColumns('confirmationToken').', '.$this->getUserColumns('timePasswordResetRequested').')
VALUES (:email, :password, :salt, :name, :roles, :timeCreated, :username, :isEnabled, :confirmationToken, :timePasswordResetRequested) ';

$params = array(
Expand Down Expand Up @@ -515,17 +542,17 @@ public function update(User $user)
$this->dispatcher->dispatch(UserEvents::BEFORE_UPDATE, new UserEvent($user));

$sql = 'UPDATE ' . $this->conn->quoteIdentifier($this->userTableName). '
SET email = :email
, password = :password
, salt = :salt
, name = :name
, roles = :roles
, time_created = :timeCreated
, username = :username
, isEnabled = :isEnabled
, confirmationToken = :confirmationToken
, timePasswordResetRequested = :timePasswordResetRequested
WHERE id = :id';
SET '.$this->getUserColumns('email').' = :email
, '.$this->getUserColumns('password').' = :password
, '.$this->getUserColumns('salt').' = :salt
, '.$this->getUserColumns('name').' = :name
, '.$this->getUserColumns('roles').' = :roles
, '.$this->getUserColumns('time_created').' = :timeCreated
, '.$this->getUserColumns('username').' = :username
, '.$this->getUserColumns('isEnabled').' = :isEnabled
, '.$this->getUserColumns('confirmationToken').' = :confirmationToken
, '.$this->getUserColumns('timePasswordResetRequested').' = :timePasswordResetRequested
WHERE '.$this->getUserColumns('id').' = :id';

$params = array(
'email' => $user->getEmail(),
Expand Down Expand Up @@ -553,10 +580,12 @@ public function update(User $user)
*/
protected function saveUserCustomFields(User $user)
{
$this->conn->executeUpdate('DELETE FROM ' . $this->conn->quoteIdentifier($this->userCustomFieldsTableName). ' WHERE user_id = ?', array($user->getId()));
$this->conn->executeUpdate('DELETE FROM ' . $this->conn->quoteIdentifier($this->userCustomFieldsTableName). '
WHERE '.$this->getUserColumns('user_id').' = ?', array($user->getId()));

foreach ($user->getCustomFields() as $attribute => $value) {
$this->conn->executeUpdate('INSERT INTO ' . $this->conn->quoteIdentifier($this->userCustomFieldsTableName). ' (user_id, attribute, value) VALUES (?, ?, ?) ',
$this->conn->executeUpdate('INSERT INTO ' . $this->conn->quoteIdentifier($this->userCustomFieldsTableName).
' ('.$this->getUserColumns('user_id').', '.$this->getUserColumns('attribute').', '.$this->getUserColumns('value').') VALUES (?, ?, ?) ',
array($user->getId(), $attribute, $value));
}
}
Expand All @@ -572,8 +601,8 @@ public function delete(User $user)

$this->clearIdentityMap($user);

$this->conn->executeUpdate('DELETE FROM ' . $this->conn->quoteIdentifier($this->userTableName). ' WHERE id = ?', array($user->getId()));
$this->conn->executeUpdate('DELETE FROM ' . $this->conn->quoteIdentifier($this->userCustomFieldsTableName). ' WHERE user_id = ?', array($user->getId()));
$this->conn->executeUpdate('DELETE FROM ' . $this->conn->quoteIdentifier($this->userTableName). ' WHERE '.$this->getUserColumns('id').' = ?', array($user->getId()));
$this->conn->executeUpdate('DELETE FROM ' . $this->conn->quoteIdentifier($this->userCustomFieldsTableName). ' WHERE '.$this->getUserColumns('user_id').' = ?', array($user->getId()));

$this->dispatcher->dispatch(UserEvents::AFTER_DELETE, new UserEvent($user));
}
Expand All @@ -592,7 +621,7 @@ public function validate(User $user)
$errors = $user->validate();

// Ensure email address is unique.
$duplicates = $this->findBy(array('email' => $user->getEmail()));
$duplicates = $this->findBy(array($this->getUserColumns('email') => $user->getEmail()));
if (!empty($duplicates)) {
foreach ($duplicates as $dup) {
if ($user->getId() && $dup->getId() == $user->getId()) {
Expand All @@ -603,7 +632,7 @@ public function validate(User $user)
}

// Ensure username is unique.
$duplicates = $this->findBy(array('username' => $user->getRealUsername()));
$duplicates = $this->findBy(array($this->getUserColumns('username') => $user->getRealUsername()));
if (!empty($duplicates)) {
foreach ($duplicates as $dup) {
if ($user->getId() && $dup->getId() == $user->getId()) {
Expand Down Expand Up @@ -676,6 +705,22 @@ public function getUserTableName()
return $this->userTableName;
}

public function setUserColumns(array $userColumns){
$conn = $this->conn;
//Escape the column names

// $escapedUserColumns = array_map(function($column) use ($conn){
// return $conn->quoteIdentifier($column,\PDO::PARAM_STR);
// }, $userColumns);
//
// //Merge the existing column names
// $this->userColumns = array_merge($this->userColumns, $escapedUserColumns);
}

public function getUserColumns($column = ""){
if ($column == "") return $this->userColumns;
else return $this->userColumns[$column];
}

public function setUserCustomFieldsTableName($userCustomFieldsTableName)
{
Expand Down
20 changes: 20 additions & 0 deletions src/SimpleUser/UserServiceProvider.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,25 @@ public function register(Application $app)
// Override table names, if necessary.
'userTableName' => 'users',
'userCustomFieldsTableName' => 'user_custom_fields',

// Override column names if necessary.
'userColumns' => array(
'id' => 'id',
'email' => 'email',
'password' => 'password',
'salt' => 'salt',
'roles' => 'roles',
'name' => 'name',
'time_created' => 'time_created',
'username' => 'username',
'isEnabled' => 'isEnabled',
'confirmationToken' => 'confirmationToken',
'timePasswordResetRequested' => 'timePasswordResetRequested',
//Custom Fields
'user_id' => 'user_id',
'attribute' => 'attribute',
'value' => 'value',
),
);

// Initialize $app['user.options'].
Expand Down Expand Up @@ -117,6 +136,7 @@ public function register(Application $app)
$userManager->setUsernameRequired($app['user.options']['isUsernameRequired']);
$userManager->setUserTableName($app['user.options']['userTableName']);
$userManager->setUserCustomFieldsTableName($app['user.options']['userCustomFieldsTableName']);
$userManager->setUserColumns($app['user.options']['userColumns']);

return $userManager;
});
Expand Down
Empty file modified src/SimpleUser/views/edit.twig
100644 → 100755
Empty file.
Empty file modified src/SimpleUser/views/email/confirm-email.twig
100644 → 100755
Empty file.
Empty file modified src/SimpleUser/views/email/reset-password.twig
100644 → 100755
Empty file.
Empty file modified src/SimpleUser/views/forgot-password.twig
100644 → 100755
Empty file.
Empty file modified src/SimpleUser/views/layout.twig
100644 → 100755
Empty file.
Empty file modified src/SimpleUser/views/list.twig
100644 → 100755
Empty file.
Empty file modified src/SimpleUser/views/login-confirmation-needed.twig
100644 → 100755
Empty file.
Empty file modified src/SimpleUser/views/login.twig
100644 → 100755
Empty file.
Empty file modified src/SimpleUser/views/register-confirmation-sent.twig
100644 → 100755
Empty file.
Empty file modified src/SimpleUser/views/register.twig
100644 → 100755
Empty file.
Empty file modified src/SimpleUser/views/reset-password.twig
100644 → 100755
Empty file.
Empty file modified src/SimpleUser/views/view.twig
100644 → 100755
Empty file.
Empty file modified tests/SimpleUser/Tests/CustomUser.php
100644 → 100755
Empty file.
Empty file modified tests/SimpleUser/Tests/MailerTest.php
100644 → 100755
Empty file.
Empty file modified tests/SimpleUser/Tests/Migration/MigrateV1ToV2Test.php
100644 → 100755
Empty file.
Empty file modified tests/SimpleUser/Tests/Migration/files/v1-sqlite.sql
100644 → 100755
Empty file.
6 changes: 6 additions & 0 deletions tests/SimpleUser/Tests/UserManagerTest.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -409,4 +409,10 @@ public function testPasswordStrengthValidator()
$error = $this->userManager->validatePasswordStrength($user, 'a');
$this->assertEquals('Password must have at least 2 characters.', $error);
}

public function testChangeUserColumns()
{
$this->userManager->setUserColumns(array('email' => 'foo'));
$this->assertEquals('"foo"', $this->userManager->getUserColumns('email'));
}
}
7 changes: 6 additions & 1 deletion tests/SimpleUser/Tests/UserServiceProviderTest.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ protected function getMinimalApp()
array('security.firewalls' => array('dummy-firewall' => array('form' => array())))
);
$app->register(new Provider\DoctrineServiceProvider());
$app->register(new UserServiceProvider());
$app->register(new UserServiceProvider(), array(
'db.options' => array(
'driver' => 'pdo_sqlite',
'memory' => true,
),
));

return $app;
}
Expand Down
Empty file modified tests/SimpleUser/Tests/UserTest.php
100644 → 100755
Empty file.