diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 diff --git a/.travis.yml b/.travis.yml old mode 100644 new mode 100755 diff --git a/LICENSE b/LICENSE old mode 100644 new mode 100755 diff --git a/README.md b/README.md old mode 100644 new mode 100755 index e88c177..21452fb --- a/README.md +++ b/README.md @@ -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 diff --git a/composer.json b/composer.json old mode 100644 new mode 100755 index 62c4d2f..61365a0 --- a/composer.json +++ b/composer.json @@ -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": "jason@grimesit.com" - } - ], + "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": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist old mode 100644 new mode 100755 diff --git a/sql/MIGRATION.md b/sql/MIGRATION.md old mode 100644 new mode 100755 diff --git a/sql/mysql.sql b/sql/mysql.sql old mode 100644 new mode 100755 diff --git a/sql/sqlite.sql b/sql/sqlite.sql old mode 100644 new mode 100755 diff --git a/src/SimpleUser/EditUserVoter.php b/src/SimpleUser/EditUserVoter.php old mode 100644 new mode 100755 diff --git a/src/SimpleUser/Mailer.php b/src/SimpleUser/Mailer.php old mode 100644 new mode 100755 diff --git a/src/SimpleUser/Migration/MigrateV1ToV2.php b/src/SimpleUser/Migration/MigrateV1ToV2.php old mode 100644 new mode 100755 diff --git a/src/SimpleUser/TokenGenerator.php b/src/SimpleUser/TokenGenerator.php old mode 100644 new mode 100755 diff --git a/src/SimpleUser/User.php b/src/SimpleUser/User.php old mode 100644 new mode 100755 diff --git a/src/SimpleUser/UserController.php b/src/SimpleUser/UserController.php old mode 100644 new mode 100755 diff --git a/src/SimpleUser/UserEvent.php b/src/SimpleUser/UserEvent.php old mode 100644 new mode 100755 diff --git a/src/SimpleUser/UserEvents.php b/src/SimpleUser/UserEvents.php old mode 100644 new mode 100755 diff --git a/src/SimpleUser/UserManager.php b/src/SimpleUser/UserManager.php old mode 100644 new mode 100755 index 002d4e9..f97a6f6 --- a/src/SimpleUser/UserManager.php +++ b/src/SimpleUser/UserManager.php @@ -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. * @@ -69,7 +88,7 @@ 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)); } @@ -77,7 +96,7 @@ public function loadUserByUsername($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)); } @@ -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'); } @@ -330,7 +354,7 @@ function isLoggedIn() */ public function getUser($id) { - return $this->findOneBy(array('id' => $id)); + return $this->findOneBy(array($this->getUserColumns('id') => $id)); } /** @@ -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); @@ -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; } @@ -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; @@ -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; } @@ -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( @@ -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(), @@ -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)); } } @@ -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)); } @@ -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()) { @@ -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()) { @@ -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) { diff --git a/src/SimpleUser/UserServiceProvider.php b/src/SimpleUser/UserServiceProvider.php old mode 100644 new mode 100755 index dc12687..f969a6c --- a/src/SimpleUser/UserServiceProvider.php +++ b/src/SimpleUser/UserServiceProvider.php @@ -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']. @@ -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; }); diff --git a/src/SimpleUser/views/edit.twig b/src/SimpleUser/views/edit.twig old mode 100644 new mode 100755 diff --git a/src/SimpleUser/views/email/confirm-email.twig b/src/SimpleUser/views/email/confirm-email.twig old mode 100644 new mode 100755 diff --git a/src/SimpleUser/views/email/reset-password.twig b/src/SimpleUser/views/email/reset-password.twig old mode 100644 new mode 100755 diff --git a/src/SimpleUser/views/forgot-password.twig b/src/SimpleUser/views/forgot-password.twig old mode 100644 new mode 100755 diff --git a/src/SimpleUser/views/layout.twig b/src/SimpleUser/views/layout.twig old mode 100644 new mode 100755 diff --git a/src/SimpleUser/views/list.twig b/src/SimpleUser/views/list.twig old mode 100644 new mode 100755 diff --git a/src/SimpleUser/views/login-confirmation-needed.twig b/src/SimpleUser/views/login-confirmation-needed.twig old mode 100644 new mode 100755 diff --git a/src/SimpleUser/views/login.twig b/src/SimpleUser/views/login.twig old mode 100644 new mode 100755 diff --git a/src/SimpleUser/views/register-confirmation-sent.twig b/src/SimpleUser/views/register-confirmation-sent.twig old mode 100644 new mode 100755 diff --git a/src/SimpleUser/views/register.twig b/src/SimpleUser/views/register.twig old mode 100644 new mode 100755 diff --git a/src/SimpleUser/views/reset-password.twig b/src/SimpleUser/views/reset-password.twig old mode 100644 new mode 100755 diff --git a/src/SimpleUser/views/view.twig b/src/SimpleUser/views/view.twig old mode 100644 new mode 100755 diff --git a/tests/SimpleUser/Tests/CustomUser.php b/tests/SimpleUser/Tests/CustomUser.php old mode 100644 new mode 100755 diff --git a/tests/SimpleUser/Tests/MailerTest.php b/tests/SimpleUser/Tests/MailerTest.php old mode 100644 new mode 100755 diff --git a/tests/SimpleUser/Tests/Migration/MigrateV1ToV2Test.php b/tests/SimpleUser/Tests/Migration/MigrateV1ToV2Test.php old mode 100644 new mode 100755 diff --git a/tests/SimpleUser/Tests/Migration/files/v1-sqlite.sql b/tests/SimpleUser/Tests/Migration/files/v1-sqlite.sql old mode 100644 new mode 100755 diff --git a/tests/SimpleUser/Tests/UserManagerTest.php b/tests/SimpleUser/Tests/UserManagerTest.php old mode 100644 new mode 100755 index 666da6a..56695d2 --- a/tests/SimpleUser/Tests/UserManagerTest.php +++ b/tests/SimpleUser/Tests/UserManagerTest.php @@ -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')); + } } diff --git a/tests/SimpleUser/Tests/UserServiceProviderTest.php b/tests/SimpleUser/Tests/UserServiceProviderTest.php old mode 100644 new mode 100755 index dcb6284..fee8992 --- a/tests/SimpleUser/Tests/UserServiceProviderTest.php +++ b/tests/SimpleUser/Tests/UserServiceProviderTest.php @@ -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; } diff --git a/tests/SimpleUser/Tests/UserTest.php b/tests/SimpleUser/Tests/UserTest.php old mode 100644 new mode 100755