Skip to content
Snippets Groups Projects
Commit c186d43d authored by Noe Nieto's avatar Noe Nieto :speech_balloon:
Browse files

Basic logic implemented

parents
No related branches found
No related tags found
No related merge requests found
*.sublime-*
\ No newline at end of file
<?php
// declare(strict_types=1);
use PHPUnit\Framework\TestCase;
include 'sanitize.php';
final class PolicyTest extends TestCase
{
public function testPolicy_usernames(): void
{
// Remove all characters after the first "@"
$this->assertEquals(
sanitize_username('foo@example.com'),
'foo'
);
// Remove all remaining non-alphanumeric characters
$this->assertEquals(
sanitize_username('valid+email@example.com'),
'validemail'
);
$this->assertEquals(
sanitize_username('valid.email@example.com'),
'validemail'
);
$this->assertEquals(
sanitize_username('valid-email@example.com'),
'validemail'
);
$this->assertEquals(
sanitize_username(')(*&l^%!o#$^l%1#3^3#7$'),
'lol1337'
);
// If username starts with a numeral, prepend an "x"
$this->assertEquals(
sanitize_username('029822c@example.com'),
'x029822c'
);
$this->assertEquals(
sanitize_username('123456789@example.com'),
'x123456789'
);
// Set all letters to lowercase
$this->assertEquals(
sanitize_username('exAmpleRWEFS@example.com'),
'examplerwefs'
);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
>
<testsuites>
<testsuite name="Purism username plugin - Test Suite">
<directory>.</directory>
</testsuite>
</testsuites>
</phpunit>
<?php
/*
* @package Purism-Username-Policy
* @version 0.1
Plugin Name: Purism's usernames policy
Plugin URI: https://source.puri.sm/liberty/internal/issues/32
Description: Wordpress creates usernames from email addresses. These could be invalid Purist usernames, for example p.Sherman. These should be autocorrected to follow Purist rules, for example psherman.
Author: Copyright 2018 Purism SPC
Version: 0.1
Author URI: http://www.puri.sm/
*/
function purism_pre_user_login_filter($sanitized_user_login){
// do your sanitization with $sanitized_user_login
return $sanitized_user_login;
}
add_filter('pre_user_login', 'purism_pre_user_login_filter', 10, 3);
<?php
/**
* Wordpress creates usernames from email addresses. These could be invalid Purist
* usernames, for example p.Sherman. These should be autocorrected to follow Purist
* rules, for example psherman.
*
* - Remove all characters after the first "@"
* - Remove all remaining non-alphanumeric characters
* - If username starts with a numeral, prepend an "x"
* - Set all letters to lowercase
* - Other Wordpress logic should apply (e.g. avoid duplicates by adding a number)
*/
function sanitize_username($login_name)
{
// Remove all characters after the first "@"
$sane_name = explode('@', $login_name)[0];
// Remove all remaining non-alphanumeric characters
$sane_name = preg_replace("/[^a-zA-Z0-9]+/", "", $sane_name);
// If username starts with a numeral, prepend an "x"
if (preg_match('/^\d/', $sane_name) === 1) {
$sane_name = 'x' . $sane_name;
}
// Set all letters to lowercase
$sane_name = strtolower($sane_name);
return $sane_name;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment