1.9. M2_User

Abstract

Interact with registered users in Moski2.net

This app interacts with registered users in Moski2.net from the client side.

M2_User::changePassword($uid, $password)

This method allows you to change a user's password directly through the API without letting the user fill in a form.

$uid is the ID of the user you wish to change the password for, and $password is the unencrypted password.

This method returns TRUE on success and FALSE on failure.

M2_User::create($data, $options)

This method allows you to create new user profiles on your site.

$data is an array of data about the user:

Table 23. Data fields for M2_User::createUser

KeyDescription
first_names

First name(s)

last_name

Last name

org_name

Organization name

email

Email

mobile

Mobile (you must prefix with a country code, as +45 for Denmark: +4510203040).

preferred_lang

Preferred language in the administration ('da' or 'en'). If the user is not going to be an administrator, just ignore this.

groups

An array of group IDs to place the user in. You can also use developer keys if you have provided them in the administration.

password

If supplied you will set the user's password to this value - otherwise a random one will be generated.

state

Initial state of the new profile: ACTIVE or SUSPENDED. Users in suspended state will not be able to log in on your site.

timezone

The timezone of the user. You can check the list of PHP supported timezones here.

This will default to the timezone of the site.

auth_type

Determines how the user will be authenticated - one of Moski2, Facebook or Google-OpenID.

Default is Moski2.


In addition to these built-in fields you can supply values for any fields you have defined in the profile XML for your site.

$options is an array of meta data about the action you are performing:

Table 24. Options fields for M2_User::createUser

KeyDescription
emailPassword

Must be TRUE or FALSE and determines whether or not the new user should have his new password emailed to him.

Note

This will only take effect if auth_type is Moski2.


The method returns the ID of the new profile. If anything goes wrong during the process, the built-in error handling will be used.

M2_User::exists($email)

You can use this to check if a user (identified by an email address) is already registered at the site. Users in all states will be matched.

The method returns a boolean - TRUE if the user exists, FALSE otherwise.

M2_User::getChangePasswordForm([$options])

This method returns a complete HTML form that allows a logged in user to change his password. If the user is not logged in the method will return FALSE.

$options is an optional hash that currently can have only one key: trans, which is another hash containing replacements for the strings used in the form. Below is a list of strings that can be overwritten. You can overwrite any number of these.

Table 25. Translation keys

KeyDescription
OLD_PASSWORD

Label for the field where the user enters his current password.

NEW_PASSWORD

Label for the field where the user enters the new password.

REPEAT_NEW_PASSWORD

Label for the field where the user repeats his new password.

CHANGE_PASSWORD

Label for the submit button of the form.

PASSWORD_MISMATCH

Error message if the new password and the repeated new password do not match.

PASSWORD_CHANGED

Confirmation message if the password is changed successfully.

PASSWORD_NOT_CHANGED

Error message if for some reason the change of password fails.

REQUIRED_FIELD

Explanation sitting next to the little star indicating a required field.

INVALID_FIELDS

Error message prefix when listing invalid input fields.

PLEASE_CORRECT

Error message suffix when listing invalid input fields.


Here is an example changing just a couple of strings:

<?php

$options = array(
                 'trans' => array(
                                  'CHANGE_PASSWORD' => 'Change it, baby',
                                  'PASSWORD_MISMATCH' => 'These are not remotely identical....'
                                 )
                );

echo M2_User::getChangePasswordForm($options);

?>
M2_User::getData($uid)

Returns a user profile as an array with the field names as keys. This includes any fields you may have added by using the profile XML mechanism.

$uid is the ID of the user you are looking for - if not supplied the session is inspected to see if there is a user logged in and then his profile data is returned.

Groups memberships are returned as an array of group IDs and group developer keys, if available.

M2_User::getLostPasswordForm([$options])

This method returns a complete HTML form that allows any registered user to retrieve a new password for the system, simply by pounding in his email address.

$options is an optional hash that currently can have only one key: trans, which is another hash containing replacements for the strings used in the form. Below is a list of strings that can be overwritten. You can overwrite any number of these.

Table 26. Translation keys

KeyDescription
EMAIL

Label for the email field.

SEND_PW

Label for the submit button of the form.

LOGIN_SENT

Text to display after the new password has been sent.

NOUSER

Text to display if the email does not match any users on the site.

EMAIL_INVALID

Text that show up if the entered email seems invalid.

USER_SUSPENDED

Text that show up if the user is suspended.

USER_SUSPENDED_BY_BOUNCE

Text that show up if the user is suspended because of bouncing e-mail.


Here is an example changing just a couple of strings:

<?php

$options = array(
                 'trans' => array(
                                  'LOGIN_SENT' => 'You gotta new password coming right at ya...',
                                  'NOUSER' => "Nope, don't know you, pal"
                                 )
                );

echo M2_User::getLostPasswordForm($options);

?>
M2_User::getStatistics()

Returns an array with a simple count of how many users exist in the four built-in states: REG_PENDING, ACTIVE, SUSPENDED and SUSPENDED_BY_BOUNCE

M2_User::isInGroup($groupID)

Checks if the currently logged in user is in the group specified by $groupID. $groupID can be either the ID of the group, or the developer key set in the administrative pages.

Returns TRUE if the user is in the queried group, FALSE if not.

M2_User::login($email, [$password], [$groupID])

Log in a user programatically. You can do this without knowing the user's password, but if you provide $password, the user must be authenticated by Moski2.net and the password must match.

If you provide a $groupID the login will only succeed if the user is in the specified group.

M2_User::search($options)

With this method you can search the user base. $options is an array of parameters - here are the keys and explanation of which values to supply:

Table 27. Options fields for M2_User::search

KeyDescriptionDefault
fields

This is an array with user fields as keys and search string as value. For example, if you want to search for all users with the first name "Peter", you could do this:

<?php

$options = array('fields' => array('first_names' => 'Peter'));
$res = M2_User::search($options);

echo '<ul>';
foreach($res as $user) {
  echo '<li>' . $user['first_names'] . ' ' . $user['last_name'] . '</li>';
}
echo '</ul>';

?>

This will print out a neat list of full names where all users have the string "Peter" in their first name.

Note

The search is case insensitive

This works for all the built-in fields - first_names, last_name, org_name, email, and mobile. It also works with your custom fields defined in the user XML.

array()
fuzzy

This determines whether you are looking for an exact match or just a string containing the string you are searching for. Set to 0 for exact matches.

1
combine

Set this to "or" if you want to get back results where just one of the fields match, or set it to "and" if all fields must match.

or
states

Acceptable states for the users matched.

Valid states are: ACTIVE, SUSPENDED, SUSPENDED_BY_BOUNCE, REG_PENDING.

array('ACTIVE')

The return value is an array of all users matching the search criteria. Each user is represented as an array with all the user fields as keys - just like M2_User::getData().

M2_User::update($uid, $data)

Use this method to update an existing user profile.

$uid is the ID of the user you wish to update and $data is an array of profile settings, exactly like the one you supply to M2_User::createUser().

Fields that are not supplied in your $data array will be left untouched, meaning that if you wish to wipe a field you should provide an empty string for it instead of leaving it out of your $data.

This method returns TRUE or FALSE depending on whether it succeeds or not. If anything goes wrong during the process, the built-in error handling will be used.