Users and Profiles

Elements fully supports Users and Profiles that allow users of your application or game to store their profile data and access a network of applications from a single login profile.

Elements uses a one-to-many model for the User / Profile relationship. A Profile is linked directly to both one single Application and one single User.

As Elements is a multi-tenant system, there can be many Applications that allow for many Profiles per Application.

This structure gives you the flexibility to create a network of Applications under the same roof, with any User level information, such as Inventory, accessible in every Application should you so choose.

User Properties

A User has the following properties:

  • id

  • name

  • email

  • level

    • UNPRIVILEGED - An unprivileged/anonymous user.

    • USER - A basic user.

    • SUPERUSER - An administrator/super user, who can do all of the above as well as delete/create users.

  • active - Inactive Users will not appear in searches made by non-superuser level users.

  • FacebookId

  • AppleSignInId

Managing Users in the Console

Users are managed in the Users section of the admin console, which can be accessed from the upper nav bar or in the hamburger menu.

The "Add User" button will open the new user panel.

Users can be edited by tapping the "Edit" button next to that user, or can be deleted by tapping the "Delete" button. Use the search function to more easily find specific users.

Passwords can be set or changed using the admin console as well. In the database, they are salted and hashed and can't be manipulated directly.

JSON Structure of Users

Here is a sample of a user JSON entry from the database:

{
    "_id" : ObjectId("5cdb0aa4e96c3c4f2bfe16ea"),
    "active" : true,
    "name" : "jsmith",
    "email" : "jsmith@elements.com",
    "level" : "SUPERUSER",
    "salt" : { "$binary" : "/thisissalt", "$type" : "00" },
    "passwordHash" : { "$binary" : "thisisapasswordhash=", "$type" : "00" },
    "hashAlgorithm" : "SHA-256"
}

Profile Properties

A Profile has the following properties:

  • Id

  • User - The User that this Profile is linked to.

  • Application - The Application that this Profile is linked to.

  • ImageUrl - Any associated avatar image.

  • DisplayName - The name that should be displayed on the client side.

  • Metadata - Key/Value store that can be modified.

  • LastLogin - The time that the current session token was created (MS since epoch)

Once created, only the DisplayName and ImageUrl can be modified from the client side (via ProfilesApi). However, other properties, such as the Metadata, can be modified from the server side.

Profile Metadata

As mentioned in the previous section, the Profile metadata is a key/value store for just about anything that you'd want to be publicly facing (as Inventory is private) in a Profile. This is a great place for storing info that you might want different Profiles to know about each other. For example, say you have a game where someone can earn points to increase a numeric rank. In order to see that information, it needs to be stored in a publicly accessible place. Take this wireframe image for example:

All of the stats listed in this screen would be housed by the Profile metadata, so the JSON representation might look something like this:

{
    "id" : <id string>, 
    "user" : <redacted>,
    "application" : <Application JSON>,
    "image_url" : <path to image>,
    "display_name" : <display name string>,
    "last_login" : <last login time>,
    "metadata": {
        "ranking" : 1300,
        "wins" : 999,
        "losses" : 999,
        "total_games" : 999,
        "current_win_streak" : 999,
        etc...
    }
}

Modifying Profile Metadata

In your Lua code, you can access the Profile DAO directly. For example:

local profile_dao = require "namazu.elements.dao.profile"

You can then use this to update the Profile metadata via one of two methods:

/**
 * Updates metadata for the Profile with the specified id, ignoring
 * changes to all other fields.     
 */
Profile updateMetadata(String profileId, Map<String, Object> metadata);

/**
 * Updates metadata for the specified Profile, ignoring changes 
 * to all other fields.
 */
Profile updateMetadata(Profile profile, Map<String, Object> metadata);

Lua tables are automatically converted to the correct Java type.

To continue the example, take this sample endpoint code:

local profile_dao = require "namazu.elements.dao.profile"
local auth = require "namazu.elements.auth"
local elements_request = require "namazu.request"
local elements_response = require "namazu.response"

local metadata_api = {}

function metadata_api.update_wins(payload, request, session)

    --
    -- Profile and User can be retrieved directly from auth for endpoints
    -- defined with 
    --  auth = { auth.SESSION_SECRET_SCHEME }
    --                  
    local profile = auth.profile()  
    local metadata = profile.metadata

    --
    -- This assumes that this endpoint was defined with
    -- parameters = {
    --      wins = { index = 1, type = "integer" }
    --  }
    --
    local updated_wins = elements_request.unpack_parameters(request, "wins")

    metadata["wins"] = updated_wins

    profile_dao.update_metadata(profile, metadata)

    return elements_response.formulate(200)
end

return metadata_api

Managing Profiles Using the Console

Profile metadata is edited identically to how item metadata is edited. See Editing Item Metadata.

Profiles can be added, edited, or deleted from the admin console in the Profiles section, accessible from the top nav bar or the hamburger menu.

Since Profiles must be connected to a user, when making a new profile you can use the "Select User" button to find the user that will own this profile. If you are editing an existing profile, you can use the "Edit User" button to open the Edit User panel for that user. Existing profiles cannot be reassigned to another user.

When creating a new profile, you must also select an Application. A dropdown menu will open with the available applications. If editing an existing profile, the Application may not be changed.

JSON Structure of Profile

This is a sample profile represented in JSON:

[{
    "_id" : ObjectId("5cdb92e7e96c3c4f2b01106f"),
    "active" : true,
    "application" : {
        "$ref" : "application",
        "$id" : ObjectId("5cdb1088e96c3c4f2bfe1da7")
    },
    "user" : {
        "$ref" : "user",
        "$id" : ObjectId("5cdb92e5e96c3c4f2b0107ca")
    },
    "imageUrl" : "url/to/image",
    "displayName" : "DisplayName",
    "lastLogin" : ISODate("2019-05-15T04:17:47.127Z"),
    "metadata" : {
        "metadata_string" : "string"
        "metadata_int" : 1
    }
}]

Last updated