Forum OpenACS Development: Re: convert person to user

Collapse
4: Re: convert person to user (response to 1)
Posted by Brian Fenton on
Hi Jeff

here's a block of code that I have that upgrades a party to a user. Could be a good starting point for you.

db_dml upgrade_user {update acs_objects set object_type = 'user' where object_id = :user_id}

db_dml insert_user {insert into users (user_id, authority_id, username, email_verified_p) values (:user_id, :authority_id, :username, 't')}

# Make sure that we we did not store user preferences before
if {![db_string user_prefs_p "select 1 from user_preferences where user_id = :user_id" -default "0"]} {
  set locale [lang::system::locale -site_wide ] 
  db_dml update_user_prefs {insert into user_preferences (user_id, locale) values (:user_id, :locale)}
} else {
  db_dml update_user_prefs {update user_preferences 
              set locale = :locale
              where user_id = :user_id }            
}

group::add_member -no_perm_check -group_id "-2" -user_id $person_id -rel_type "membership_rel"

if {[empty_string_p $password]} {
  # we reset the password in admin mode. this means that an email
  # will not automatically be sent unless acs-subsite.EmailChangedPasswordP is 1.

  array set passwordresults [auth::password::reset -authority_id [auth::authority::local] -username $username -admin]

  set password "$passwordresults(password)"
} else {
  # Change the password to the password provided

  if { [catch { ad_change_password $user_id $password } errmsg] } {
    ns_log notice "Error changing local password for username $username, user_id $user_id: \n$errorInfo"
  }
}

# Grant the user to update the password on himself
permission::grant -party_id $user_id -object_id $user_id -privilege write