Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:spira/spira into hotfix/email-change
# By Johan Steen (61) and Zak Henry (19) # Via Johan Steen (7) and others * 'master' of github.com:spira/spira: (80 commits) Defines zip dists for all repositories Uses https instead of ssh for all repositories Dropped duplicate comment Added %expose_port% so the QA runner can rewrite the compose file to it's desired port Added docker-compose files for build and prod, reconfigured the qa file to use services Extracted out common-services to a separate file Bumped bower version for angular-jwt-auth Fixed auth test to use correct cookie name Added forum redirection, fixed the bower versions to stop master from failing Restored .gitkeep file Bumped lodash version up, added resolution Fixed sso integration test to use cookie name constant Upgraded lodash, changed cookie name, implemented config for saving of the cookie to the top level domain. Reverted to ~1.4 Attempting with latest animation update Upgrade to angular ~1.4 Switched forum port to 80, removed proxypass Removes cookie generation Sets a Database placeholder entry Restored hhvm ... # Conflicts: # api/tests/integration/UserTest.php
- Loading branch information
Showing
59 changed files
with
3,742 additions
and
217 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
<?php | ||
|
||
namespace App\Extensions\Socialite\Parsers; | ||
|
||
use App\Models\User; | ||
|
||
trait UsernameTrait | ||
{ | ||
/** | ||
* Get the user's username. | ||
* | ||
* @return string | ||
*/ | ||
abstract protected function getUsernameAttribute(); | ||
|
||
/** | ||
* Define filter for usernames. | ||
* | ||
* @param string $username | ||
* | ||
* @return string | ||
*/ | ||
protected function filterUsernameAttribute($username) | ||
{ | ||
if ($this->usernameIsUnique($username)) { | ||
return $username; | ||
} | ||
|
||
return $this->makeUsernameUnique($username); | ||
} | ||
|
||
/** | ||
* Check if the username is unique. | ||
* | ||
* @param string $username | ||
* | ||
* @return bool | ||
*/ | ||
protected function usernameIsUnique($username) | ||
{ | ||
return ! (bool) User::username($username)->get()->count(); | ||
} | ||
|
||
/** | ||
* Make sure that the username is unique for Spira. | ||
* | ||
* The username retrieved from the social provider is not guaranteed to be | ||
* unique for Spira. So to ensure a seamless social signup and signin | ||
* process, modify the username if it is not unique to be unique. | ||
* | ||
* @param string $username | ||
* | ||
* @return string | ||
*/ | ||
protected function makeUsernameUnique($username) | ||
{ | ||
// Try prettye methods first, ordered by priority | ||
$methods = [ | ||
'removeSpaces', | ||
'replaceSpacesWithDots', | ||
'switchFirstLast', | ||
'InitialAndLast' | ||
]; | ||
|
||
foreach ($methods as $method) { | ||
$method = camel_case('username_'.$method); | ||
if (method_exists($this, $method)) { | ||
$tryName = $this->$method($username); | ||
|
||
if ($this->usernameIsUnique($tryName)) { | ||
return $tryName; | ||
} | ||
} | ||
} | ||
|
||
// None of the pretty methods were successful, start numbering | ||
$suffix = 1; | ||
do { | ||
$tryName = $username.' '.$suffix; | ||
$suffix++; | ||
} while (!$this->usernameIsUnique($tryName)); | ||
|
||
return $tryName; | ||
} | ||
|
||
/** | ||
* Removes spaces from username. | ||
* | ||
* @param string $username | ||
* | ||
* @return string | ||
*/ | ||
protected function usernameRemoveSpaces($username) | ||
{ | ||
return str_replace(' ', '', $username); | ||
} | ||
|
||
/** | ||
* Removes spaces from username. | ||
* | ||
* @param string $username | ||
* | ||
* @return string | ||
*/ | ||
protected function usernameReplaceSpacesWithDots($username) | ||
{ | ||
return str_replace(' ', '.', $username); | ||
} | ||
|
||
/** | ||
* Switch position of first and last name. | ||
* | ||
* @param string $username | ||
* | ||
* @return string | ||
*/ | ||
protected function usernameSwitchFirstLast($username) | ||
{ | ||
$segments = explode(' ', $username); | ||
if (count($segments) == 2) { | ||
return implode(' ', array_reverse($segments)); | ||
} | ||
|
||
// The username was not suitable for this operation, return unmodified | ||
return $username; | ||
} | ||
|
||
/** | ||
* Use first name initial and last name. | ||
* | ||
* @param string $username | ||
* | ||
* @return string | ||
*/ | ||
protected function usernameInitialAndLast($username) | ||
{ | ||
$segments = explode(' ', $username); | ||
if (count($segments) == 2) { | ||
return $segments[0][0].$segments[1]; | ||
} | ||
|
||
// The username was not suitable for this operation, return unmodified | ||
return $username; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.