Forums | Mahara Community

Support /
Disable username changes


anonymous profile picture
Account deleted
Posts: 31

16 May 2013, 16:08

Hi Guys,

is there a way to stop users from changing their username once the account has been created. I normally import students usernames from a .csv which is basically there First and last name combined. but sometimes they are changing their usernames to something completely differerent.

I know there is no option from within mahara, but what about in the php?

Aaron Wells's profile picture
Posts: 896

21 May 2013, 17:21

Sure, you could just delete the relevant parts from htdocs/account/index.php. This would probably do it.

diff --git a/htdocs/account/index.php b/htdocs/account/index.php
index 44aa121..12b73c4 100644
--- a/htdocs/account/index.php
+++ b/htdocs/account/index.php
@@ -73,18 +73,6 @@ else {
     $elements = array();
 }
 
-if ($authobj->authname == 'internal') {
-    $elements['changeusernameheading'] = array(
-        'value' => '<tr><td colspan="2"><h3>' . get_string('changeusernameheading', 'account') . '</h3></td></tr>'
-    );
-    $elements['username'] = array(
-        'type' => 'text',
-        'defaultvalue' => $USER->get('username'),
-        'title' => get_string('changeusername', 'account'),
-        'description' => get_string('changeusernamedesc', 'account', hsc(get_config('sitename'))),
-    );
-}
-
 if (get_config('cleanurls') && get_config('cleanurlusereditable')) {
     $elements['changeprofileurl'] = array(
         'value' => '<tr><td colspan="2"><h3>' . get_string('changeprofileurl', 'account') . '</h3></td></tr>'
@@ -238,12 +226,6 @@ function accountprefs_submit(Pieform $form, $values) {
 
     $returndata = array();
 
-    if (isset($values['username']) && $values['username'] != $USER->get('username')) {
-        $USER->username = $values['username'];
-        $USER->commit();
-        $returndata['username'] = $values['username'];
-    }
-
     if (get_config('cleanurls') && isset($values['urlid']) && $values['urlid'] != $USER->get('urlid')) {
         $USER->urlid = $values['urlid'];
         $USER->commit();

Just make sure you delete the code that handles it when they submit a new username (that's the second bit I've deleted), and not just the code that displays the username field. Otherwise, technically proficient users would still be able to change their username by editing their page request manually.

Cheers,
Aaron

anonymous profile picture
Account deleted
Posts: 31

22 May 2013, 14:35

Hi,

Thanks very much for the reply, but i am having some trouble understand what bits of the script to edit.

I have removed the following out of the index,php:

-if ($authobj->authname == 'internal') {
-    $elements['changeusernameheading'] = array(
-        'value' => '<tr><td colspan="2"><h3>' . get_string('changeusernameheading', 'account') . '</h3></td></tr>'
-    );
-    $elements['username'] = array(
-        'type' => 'text',
-        'defaultvalue' => $USER->get('username'),
-        'title' => get_string('changeusername', 'account'),
-        'description' => get_string('changeusernamedesc', 'account', hsc(get_config('sitename'))),
-    );
-}
-
-    if (isset($values['username']) && $values['username'] != $USER->get('username')) {
-        $USER->username = $values['username'];
-        $USER->commit();
-        $returndata['username'] = $values['username'];
-    }
-
     if (get_config('cleanurls') && isset($values['urlid']) && $values['urlid'] != $USER->get('urlid')) {
         $USER->urlid = $values['urlid'];
         $USER->commit();


But i am unsure of what do do with these parts:

diff --git a/htdocs/account/index.php b/htdocs/account/index.php
index 44aa121..12b73c4 100644
--- a/htdocs/account/index.php
+++ b/htdocs/account/index.php
@@ -73,18 +73,6 @@ else {
     $elements = array();
 }
 if (get_config('cleanurls') && get_config('cleanurlusereditable')) {
     $elements['changeprofileurl'] = array(
         'value' => '<tr><td colspan="2"><h3>' . get_string('changeprofileurl', 'account') . '</h3></td></tr>'
@@ -238,12 +226,6 @@ function accountprefs_submit(Pieform $form, $values) {

Currently the username field has gone, but when i click save it just hangs.

Thanks for your help

Aaron Wells's profile picture
Posts: 896

22 May 2013, 16:09

Hi Matthew,

No worries! Laughing The text I provided you was the output from the git-diff command, what's called a "unified diff" ( http://en.wikipedia.org/wiki/Diff#Unified_format ). Basically, you delete the lines that are marked with a minus, add lines marked with a + (there aren't any in this case), and the rest is just there for context. The lines starting with "@@" are there to tell you what line number in the file the following section is meant to represent.

You could take also what I've pasted in, save it into a file called patch.txt, and apply that patch to your copy of Mahara using the "patch" command-line utility: "patch -p1 < patch.txt".

From what you've posted, the problem may be that you deleted a portion of lines that were not preceeded with minuses. Specifically, this part:

     if (get_config('cleanurls') && isset($values['urlid']) && $values['urlid'] != $USER->get('urlid')) {
         $USER->urlid = $values['urlid'];
         $USER->commit();

If you've deleted that, it will cause a syntax error, which will indeed cause problems. Wink

Cheers,
Aaron

anonymous profile picture
Account deleted
Posts: 31

23 May 2013, 12:04

That Aaron,

I have edited the .php but am still encountering the hanging when a user tries to change other settings in their settings page and press save. The "loading" in the top left corner just stays there and the page doesnt move.


Could you please check my index.php to see if i have missed something?

http://tny.cz/43988a19

 

Thanks for all your help

Mathew

Aaron Wells's profile picture
Posts: 896

24 May 2013, 13:05

Ah, it looks like there was one more section that needed to be deleted, from the validation function in that page. Try this version of the file on for size.  https://mahara.org/view/artefact.php?artefact=268850&view=81194

This time I actually tested it, instead of just reading the code. Wink

Cheers,
Aaron

anonymous profile picture
Account deleted
Posts: 31

24 May 2013, 14:12

Thanks aaron!

That works perfect!!!

Appriciate all your help with this issue.

Regards,
Mathew

Zoltan Csernai's profile picture
Posts: 9

18 December 2016, 10:41

Hi Aaron,

I install the Mahara 16.10.1 version and I want disable username changes.

I delete the relevant parts from htdocs/account/index.php:

if ($authobj->authname == 'internal') {
    $elements['changeusernameheading'] = array(
        'value' => '<tr><td colspan="2"><h3>' . get_string('changeusernameheading', 'account') . '</h3></td></tr>'
    );
    $elements['username'] = array(
        'type' => 'text',
        'defaultvalue' => $USER->get('username'),
        'title' => get_string('changeusername', 'account'),
        'description' => get_string('changeusernamedesc', 'account', hsc(get_config('sitename'))),
    );
}

I delete the following lines too:

if (isset($values['username']) && $values['username'] != $USER->get('username')) {
        $USER->username = $values['username'];
        $USER->commit();
        $returndata['username'] = $values['username'];
    }

The lines, that you marked with a + I found in the index.php.

How can I fix the index.php?

Thanks,

Zoltan

A post by Account deleted was deleted

9 results