/**
 * Sets the changeset's user id by looking up email address or name
 * Generic code for both Author and Committer, based on the given properties
 * @param object $p_changeset
 * @param string $p_user_type 'author' or 'committer'
 */
function Source_set_changeset_user(&$p_changeset, $p_user_type)
{
    static $s_vcs_names;
    static $s_names = array();
    static $s_emails = array();
    # Set the fields
    switch ($p_user_type) {
        case 'committer':
            list($t_id_prop, $t_name_prop, $t_email_prop) = explode(' ', 'committer_id committer committer_email');
            break;
        case 'author':
        default:
            list($t_id_prop, $t_name_prop, $t_email_prop) = explode(' ', 'user_id author author_email');
            break;
    }
    # The user's id is already set, nothing to do
    if ($p_changeset->{$t_id_prop}) {
        return;
    }
    # cache the vcs username mappings
    if (is_null($s_vcs_names)) {
        $s_vcs_names = SourceUser::load_mappings();
    }
    # Check username associations
    if (isset($s_vcs_names[$p_changeset->{$t_name_prop}])) {
        $p_changeset->{$t_id_prop} = $s_vcs_names[$p_changeset->{$t_name_prop}];
        return;
    }
    # Look up the email address if given
    if ($t_email = $p_changeset->{$t_email_prop}) {
        if (isset($s_emails[$t_email])) {
            $p_changeset->{$t_id_prop} = $s_emails[$t_email];
            return;
        } else {
            if (false !== ($t_email_id = user_get_id_by_email($t_email))) {
                $s_emails[$t_email] = $p_changeset->{$t_id_prop} = $t_email_id;
                return;
            }
        }
    }
    # Look up the name if the email failed
    if ($t_name = $p_changeset->{$t_name_prop}) {
        if (isset($s_names[$t_name])) {
            $p_changeset->{$t_id_prop} = $s_names[$t_name];
            return;
        } else {
            if (false !== ($t_user_id = user_get_id_by_realname($t_name))) {
                $s_names[$t_name] = $p_changeset->{$t_id_prop} = $t_user_id;
                return;
            } else {
                if (false !== ($t_user_id = user_get_id_by_name($p_changeset->{$t_name_prop}))) {
                    $s_names[$t_name] = $p_changeset->{$t_id_prop} = $t_user_id;
                    return;
                }
            }
        }
    }
}
 /**
  * Add mapping repository_user -> activecollab user
  *
  * @param void
  * @return null
  */
 function repository_user_add()
 {
     if (!$this->active_repository->canEdit($this->logged_user)) {
         $this->httpError(HTTP_ERR_FORBIDDEN, null, true);
     }
     // if
     if (!$this->request->isSubmitted()) {
         $this->httpError(HTTP_ERR_BAD_REQUEST, null, true);
     }
     //if
     if (!$this->request->isAsyncCall()) {
         $this->httpError(HTTP_ERR_BAD_REQUEST, null, true);
     }
     //if
     $source_user = new SourceUser();
     $source_user->setRepositoryId($this->active_repository->getId());
     $source_user->setRepositoryUser($this->request->post('repository_user'));
     $source_user->setUserId($this->request->post('user_id'));
     // validation is moved here because the management form is not on separate page
     if (!$source_user->validatePresenceOf('user_id')) {
         die('error');
     }
     // if
     $save = $source_user->save();
     if ($save && !is_error($save)) {
         $source_user->setSystemUser();
         $this->smarty->assign('source_user', $source_user);
         $this->smarty->display(get_template_path('_repository_user_row', 'repository', SOURCE_MODULE));
         die;
     } else {
         die('error');
     }
     // if
 }
 /**
  * When updating user preferences, allowing the user or admin to specify
  * a version control username to be associated with the account.
  * @param string Event name
  * @param int User ID
  */
 function account_update($p_event, $p_user_id)
 {
     if (!access_has_global_level(config_get('plugin_Source_username_threshold'))) {
         return;
     }
     $f_vcs_sent = gpc_get_bool('Source_vcs', false);
     $f_vcs_username = gpc_get_string('Source_vcs_username', '');
     # only load and persist the username if things are set and changed
     if ($f_vcs_sent) {
         $t_user = SourceUser::load($p_user_id);
         if ($t_user->username != $f_vcs_username) {
             $t_user->username = $f_vcs_username;
             $t_user->save();
         }
     }
 }
/**
 * Determine the user ID for both the author and committer.
 * First checks the email address for a matching user, then
 * checks the name for a matching username or realname.
 * @param object Changeset object
 */
function Source_Parse_Users($p_changeset)
{
    static $s_vcs_names;
    static $s_names = array();
    static $s_emails = array();
    # cache the vcs username mappings
    if (is_null($s_vcs_names)) {
        $s_vcs_names = SourceUser::load_mappings();
    }
    # Handle the changeset author
    while (!$p_changeset->user_id) {
        # Check username associations
        if (isset($s_vcs_names[$p_changeset->author])) {
            $p_changeset->user_id = $s_vcs_names[$p_changeset->author];
            break;
        }
        # Look up the email address if given
        if ($t_email = $p_changeset->author_email) {
            if (isset($s_emails[$t_email])) {
                $p_changeset->user_id = $s_emails[$t_email];
                break;
            } else {
                if (false !== ($t_email_id = user_get_id_by_email($t_email))) {
                    $s_emails[$t_email] = $p_changeset->user_id = $t_email_id;
                    break;
                }
            }
        }
        # Look up the name if the email failed
        if ($t_name = $p_changeset->author) {
            if (isset($s_names[$t_name])) {
                $p_changeset->user_id = $s_names[$t_name];
                break;
            } else {
                if (false !== ($t_user_id = user_get_id_by_realname($t_name))) {
                    $s_names[$t_name] = $p_changeset->user_id = $t_user_id;
                    break;
                } else {
                    if (false !== ($t_user_id = user_get_id_by_name($p_changeset->author))) {
                        $s_names[$t_name] = $p_changeset->user_id = $t_user_id;
                        break;
                    }
                }
            }
        }
        # Don't actually loop
        break;
    }
    # Handle the changeset committer
    while (!$p_changeset->committer_id) {
        # Check username associations
        if (isset($s_vcs_names[$p_changeset->committer])) {
            $p_changeset->user_id = $s_vcs_names[$p_changeset->committer];
            break;
        }
        # Look up the email address if given
        if ($t_email = $t_email) {
            if (isset($s_emails[$t_email])) {
                $p_changeset->committer_id = $s_emails[$t_email];
                break;
            } else {
                if (false !== ($t_email_id = user_get_id_by_email($t_email))) {
                    $s_emails[$t_email] = $p_changeset->committer_id = $t_email_id;
                    break;
                }
            }
        }
        # Look up the name if the email failed
        if ($t_name = $p_changeset->committer) {
            if (isset($s_names[$t_name])) {
                $p_changeset->committer_id = $s_names[$t_name];
                break;
            } else {
                if (false !== ($t_user_id = user_get_id_by_realname($t_name))) {
                    $s_names[$t_name] = $p_changeset->committer_id = $t_user_id;
                    break;
                } else {
                    if (false !== ($t_user_id = user_get_id_by_name($t_name))) {
                        $s_names[$t_name] = $p_changeset->committer_id = $t_user_id;
                        break;
                    }
                }
            }
        }
        # Don't actually loop
        break;
    }
    return $p_changeset;
}