Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
266 changes: 215 additions & 51 deletions GoogleOauth.php
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,55 +1,219 @@
<?php

class GoogleOauthPlugin extends MantisPlugin {

var $cmv_pages;
var $current_page;

function register() {
$this->name = 'Google Authentication Module';
$this->description = 'Add Google authentication to MantisBT.';
$this->page = 'config';

$this->version = '2.0';
$this->requires = array(
'MantisCore' => '2.0.0',
);

$this->author = 'Alleen Wang';
$this->contact = '[email protected]';
$this->url = 'http://alleen.tw';
}

function init() {
$this->cmv_pages = array(
'login_page.php'
);
$this->current_page = basename( $_SERVER['PHP_SELF'] );
}

function hooks() {
return array(
'EVENT_LAYOUT_RESOURCES' => 'resources'
);
}

function config() {
return array(
'clientId' => '',
'clientSecret' => '',
'redirect_uri' => '',
);
}

function resources() {
if ( ! in_array( $this->current_page, $this->cmv_pages ) ) {
return '';
}

return '
<meta name="redirectUri" content="' . plugin_config_get( 'redirect_uri' ) . '" />
<meta name="clientId" content="' . plugin_config_get( 'clientId' ) . '" />
<script type="text/javascript" src="plugins/GoogleOauth/pages/assets/js/plugin.js"></script>
';
}
var $cmv_pages;
var $current_page;

function register() {
$this->name = 'Google Authentication Module';
$this->description = 'Add Google authentication to MantisBT.';
$this->page = 'config';

$this->version = '2.0.2';
$this->requires = array(
'MantisCore' => '2.0.0',
);

$this->author = 'Alleen Wang';
$this->contact = '[email protected]';
$this->url = 'http://alleen.tw';
}

function init() {
$this->cmv_pages = array(
'login_page.php'
);
$this->current_page = basename( $_SERVER['PHP_SELF'] );
plugin_require_api( 'core/user_api.php' );
}

function hooks() {
return array(
'EVENT_LAYOUT_RESOURCES' => 'resources',
'EVENT_MANAGE_USER_CREATE_FORM' => 'oauthEmailInputForCreate',
'EVENT_MANAGE_USER_UPDATE_FORM' => 'oauthEmailInputForEdit',
'EVENT_MANAGE_USER_UPDATE' => 'saveGmailAddress',
'EVENT_MANAGE_USER_CREATE' => 'saveGmailAddress'
);
}


function config() {
return array(
'clientId' => '',
'clientSecret' => '',
'redirect_uri' => '',
);
}

function resources() {
if ( ! in_array( $this->current_page, $this->cmv_pages ) ) {
return '';
}

$redirectUri = plugin_config_get( 'redirect_uri' );
$clientId = plugin_config_get( 'clientId' );

$res = '<meta name="redirectUri" content="' . $redirectUri . '" />';
$res .= '<meta name="clientId" content="' . $clientId . '" />';
$res .= '<script type="text/javascript" ' .
' src="' . plugin_file( 'plugin.js' ) . '"></script> ';

return $res;
}

/**
*
*
*/
function oauthEmailInputForCreate( $p_event, $p_user_id = null ) {
$this->oauthEmailInput($p_user_id,'create');
}

/**
*
*
*/
function oauthEmailInputForEdit( $p_event, $p_user_id = null ) {
$this->oauthEmailInput($p_user_id,'edit');
}

/**
*
*
*/
function oauthEmailInput( $p_user_id = null, $operation = null ) {

switch( $operation ) {
case 'edit':
$str_open = $str_close = '';
$table = plugin_table( 'user' );
$t_query = " SELECT * FROM {$table} WHERE user_id=" . db_param();
$t_sql_param = array( $p_user_id );
$t_result = db_query( $t_query, $t_sql_param);
$t_row = db_fetch_array( $t_result );
$attr['gmail_address'] = $t_row['gmail_address'];
break;

case 'create':
default:
$str_open = '<p><table class="table table-bordered table-condensed table-striped">' . '<fieldset>';
$str_close = '</fieldset></table>';
$attr['gmail_address'] = null;
break;
}

echo $str_open;
$this->draw_oauth_email_input_row( $attr );
echo $str_close;
}

/**
*
*/
function draw_oauth_email_input_row($attr=null) {

$attribute = $attr;
$attribute['size'] = 32;
$attribute['maxlength'] = 64;

$this->draw_generic_input_row('gmail_address',$attribute,'');
}

/**
*
*
*/
function draw_generic_input_row($item_idcard,$attr=null, $suffix='_code') {
$lbl = plugin_lang_get($item_idcard);

$access_key = "{$item_idcard}{$suffix}";
$input_name = "plugin_{$access_key}";
$value = !is_null($attr[$item_idcard]) ? $attr[$item_idcard] : '';
echo '<tr ', helper_alternate_class(), '><td class="category">', $lbl,'</td>';
echo '<td>';
echo '<input type="text" id="' . $input_name . '"' .
' name="' . $input_name . '"' . ' value="' . $value . '"';

echo ' class="input-sm" ';

if( isset($attr['size']) ) {
echo ' size="' . intval($attr['size']) . '" ';
}

if( isset($attr['maxlength']) ) {
echo ' maxlength="' . intval($attr['maxlength']) . '" ';
}

echo '>';
echo '</td></tr>';
}


/**
*
*/
function saveGmailAddress( $p_event, $p_user_id ) {

// Get User data
$gmail_address = '';
if( isset($_REQUEST['plugin_gmail_address']) ) {
$gmail_address = trim($_REQUEST['plugin_gmail_address']);
}

// Insert or Update ?
$table = plugin_table('user');

db_param_push();
$t_query = "SELECT user_id,gmail_address
FROM {$table} WHERE user_id=" . db_param();
$t_result = db_query( $t_query, array( $p_user_id ) );
$t_row_count = db_num_rows($t_result);

$t_sql_param = array($gmail_address,$p_user_id);

$t_doDelete = (null == $gmail_address && '' == trim($gmail_address));
$t_doInsert = !$t_doDelete;

if( $t_row_count == 1 ) {
// if new gmail_address is empty => delete the record, because
// we have a UNIQUE INDEX on gmail_address
// $t_row = db_fetch_array( $t_result );
if( $t_doDelete ) {
$t_query = " DELETE FROM {$table} ";
$t_sql_param = array($p_user_id);
} else {
$t_query = " UPDATE {$table} SET gmail_address = " . db_param();
}
$t_query .= " WHERE user_id=" . db_param();

} else {
$t_query = null;
if( $t_doInsert ) {
$t_query = " INSERT INTO {$table} (gmail_address,user_id) ";
$t_query .= " VALUES(" . db_param() . ',' . db_param() . ") ";
}
}
if( null != $t_query ) {
db_query( $t_query, $t_sql_param );
}
}


/**
*
*/
function schema() {
$t_ddl = " user_id I NOTNULL UNSIGNED PRIMARY," .
" gmail_address C(200) NULL DEFAULT \" '' \" ";

$t_schema[] = array( 'CreateTableSQL',
array( plugin_table( 'user' ), $t_ddl)
);

$t_schema[] = array( 'CreateIndexSQL', array( 'idx_gmail_address', plugin_table( 'user' ), 'gmail_address', array( 'UNIQUE' ) ) );

return $t_schema;
}

}
30 changes: 29 additions & 1 deletion README.md
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# MantisBT GoogleAuth Plugin
--------


[TOC]


Features
--------
1. Add Google oauth 2.0 support to login to MantisBT.
Expand Down Expand Up @@ -25,5 +32,26 @@ Supported Versions
------------------

- MantisBT 1.2.x - supported
- MantisBT 1.3.x - **not supported**
- MantisBT 2.x - supported (repository master branch)

Plugin Folder Structure
-----------------------
Struct has been changed to follow the MantisBT suggested folder structure and naming convention

./GoogleAuth/pages
./GoogleAuth/files
./GoogleAuth/library

Operations Flow
-----------------------
If installation was ok, when you access MantisBT login page you will see a new button

![](.//screens/login_screen_with_sign_in_with_google_button.png)

When you click on button 'Sign in with google' you will see something similar to:

![](.//screens/google_signin_screen.png)

Or this

![](.//screens/google_account_choice.png)
37 changes: 37 additions & 0 deletions core/user_api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* User API
*
*/

use Mantis\Exceptions\ClientException;

/**
* Get a user id from their GMAIL email address
*
* @param string $p_email The email address to retrieve data for.
* @param boolean $p_throw true to throw exception when not found, false otherwise.
* @return array
*/
function user_get_id_by_gmail_address( $p_email, $p_throw = false ) {

$table = plugin_table('user');

db_param_push();
$t_query = "SELECT user_id AS id FROM {$table} WHERE gmail_address=" . db_param();
$t_result = db_query( $t_query, array( $p_email ) );

$t_row = db_fetch_array( $t_result );
if( $t_row ) {
return $t_row['id'];
}

if( $p_throw ) {
throw new ClientException(
"User with gmail_address '$p_email' not found",
ERROR_USER_BY_EMAIL_NOT_FOUND,
array( $p_email ) );
}

return false;
}
File renamed without changes.
20 changes: 18 additions & 2 deletions lang/strings_english.txt
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,21 @@ $s_plugin_GoogleOauth_title = 'Google Oauth Setting';
$s_plugin_GoogleOauth_save = 'SAVE';

$s_plugin_GoogleOauth_login = 'Sign in with Google';
$s_plugin_AdminSetPassword_password_length = 'Enter a password with at least 6 characters.';
$s_plugin_AdminSetPassword_password_success = 'New password has been set successfully.';

$s_plugin_GoogleOauth_user_is_enabled_failure =
"<p>Email address not registered. Please register new account first. (on your mantis) <br/>";

$s_plugin_GoogleOauth_user_is_login_request_allowed_failure =
"<p>Email address not registered. Please register new account first. (on your mantis) <br/>";


$s_action_common =
'<a class="btn btn-primary btn-sm bigger-110" href="$$basehref$$/login_page.php">MantisBT Login Page</a>';

$s_plugin_GoogleOauth_user_is_anonymous = $s_action_common;

$s_plugin_GoogleOauth_user_is_enabled_failure_action = $s_action_common;

$s_plugin_GoogleOauth_user_is_login_request_allowed_failure_action =$s_action_common;

$s_plugin_GoogleOauth_gmail_address = 'Gmail Address';
Empty file modified lang/strings_french.txt
100755 → 100644
Empty file.
4 changes: 4 additions & 0 deletions library/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
GoogleAuth Plugin external libraries
====================================

This directory contains a copy the 3rd-party libraries used by GoogleAuth.
Loading