acs_user::promote_person_to_user (public)

 acs_user::promote_person_to_user [ -person_id person_id ] \
    [ -authority_id authority_id ] [ -username username ] \
    [ -password password ] [ -locale locale ]

Defined in packages/acs-tcl/tcl/community-core-procs.tcl

Promotes a person/party to an ACS user.

Switches:
-person_id
(optional)
the person_id in the acs system that should be promoted to a user.
-authority_id
(optional)
the authority that will be used for the user.
-username
(optional)
the username to be used for this user. Defaults to the person's email
-password
(optional)
the password to be used for this user. Defaults to a randomly generated password.
-locale
(optional)
locale to be used in user preferences. Defaults to the site wide locale is taken.
Returns:
The user_id of the person promoted to user
Error:
An error is thrown if the username is already in use, or the person_id has no email address, or if person_id is not in the persons table.
See Also:

Partial Call Graph (max 5 caller/called nodes):
%3 test_demote_promote_a_user demote_promote_a_user (test acs-tcl) acs_user::promote_person_to_user acs_user::promote_person_to_user test_demote_promote_a_user->acs_user::promote_person_to_user acs_user::flush_cache acs_user::flush_cache (public) acs_user::promote_person_to_user->acs_user::flush_cache acs_user::get_by_username acs_user::get_by_username (public) acs_user::promote_person_to_user->acs_user::get_by_username db_dml db_dml (public) acs_user::promote_person_to_user->db_dml db_exec_plsql db_exec_plsql (public) acs_user::promote_person_to_user->db_exec_plsql db_transaction db_transaction (public) acs_user::promote_person_to_user->db_transaction

Testcases:
demote_promote_a_user
Source code:

    if { $username eq "" } {
        #
        # Take the email as username, if no username was provided.
        #
        set party_info [party::get -party_id $person_id]
        if {[llength $party_info] > 0} {
            set username [string tolower [dict get $party_info email]]
        }
        if { $username eq "" } {
            error "The party to be promoted does either not exist or has no email address"
        }
    }

    #
    # Make sure this username is not already in use.
    #
    set user_id [acs_user::get_by_username -authority_id $authority_id -username $username]
    if {$user_id ne ""} {
        error "The username '$username' is already in use."
    }

    #
    # The person to be promoted has to be a valid person
    #
    set person_info [person::get -person_id $person_id]
    if {[llength $party_info] == 0} {
        error "No person with person_id $person_id defined"
    }

    #
    # Determine locale to be used in the user preferences
    #
    if {$locale eq ""} {
        set locale [lang::system::locale -site_wide]
    }

    db_transaction {
        #
        # Set up variables for the new user.
        #
        set first_names [dict get $person_info first_names]
        set last_name [dict get $person_info last_name]

        #
        # Generate salt
        #
        set salt [sec_random_token]
        #
        # If password was not passed in, generate that too.
        #
        if { $password == "" } {
            set password [sec_random_token]
        }
        set hashed_password [ns_sha1 "$password$salt"]

        ns_log notice "CALL  acs_user__new(:person_id, --user_id
                                 'user', --object_type
                                 now(), --creation_date
                                 null, --creation_user
                                 null, --creation_ip
                                 null, --authority_id
                                 :username, --username
                                 null, --email
                                 null, --url
                                 :first_names, --first_names
                                 :last_name, --last_name
                                 :hashed_password, --password
                                 :salt, --salt
                                 null, --screen_name
                                 't', --email_verified_p
                                 null --context_id
                                 )"
        db_exec_plsql noxql {
            SELECT acs_user__new(:person_id, --user_id
                                 'user', --object_type
                                 now(), --creation_date
                                 null, --creation_user
                                 null, --creation_ip
                                 null, --authority_id
                                 :username, --username
                                 null, --email
                                 null, --url
                                 :first_names, --first_names
                                 :last_name, --last_name
                                 :hashed_password, --password
                                 :salt, --salt
                                 null, --screen_name
                                 't', --email_verified_p
                                 null --context_id
                                 )
        }
        # Add user to 'registered_users' group and set permissions
        db_exec_plsql noxql {
            SELECT membership_rel__new (
                            null,
                            'membership_rel',
                            acs__magic_object_id('registered_users'),
                            :person_id,
                            'approved',
                            null,
                            null)
        }
        #
        # Update user preferences
        #
        db_dml noxql {
            update user_preferences
            set locale = :locale
            where user_id = :person_id
        }

        #
        # Update object type.
        #
        db_dml update_object_type {
            UPDATE acs_objects
            SET    object_type = 'user'
            WHERE  object_id = :person_id
        }

        # A user needs read and write permissions on themselves
        permission::grant -party_id $person_id -object_id $person_id -privilege "read"
        permission::grant -party_id $person_id -object_id $person_id -privilege "write"
    }

    #
    # Flush the cache. It should not be necessary to flush the
    # person_info cache, since the "person" is still around.
    #
    acs_user::flush_cache -user_id $person_id

    return $person_id
Generic XQL file:
packages/acs-tcl/tcl/community-core-procs.xql

PostgreSQL XQL file:
packages/acs-tcl/tcl/community-core-procs-postgresql.xql

Oracle XQL file:
packages/acs-tcl/tcl/community-core-procs-oracle.xql

[ hide source ] | [ make this the default ]
Show another procedure: