9bde87fa by Chris Boden

Updated Auth library with settings, added Facebook settings

1 parent 2dff1936
......@@ -2,7 +2,9 @@
namespace Tz\WordPress\Tools\Auth;
use \Exception, \LogicException, \InvalidArgumentException, \BadMethodCallException;
use Tz\WordPress\Tools;
use Tz\Common;
use Exception, LogicException, InvalidArgumentException, BadMethodCallException;
const REG_METH_AUTO_REG = 1;
const REG_METH_VALID_EMAIL = 2;
......@@ -12,6 +14,24 @@ const FORGOT_METH_RAND_PASS = 2;
const ACTION_ACTIVATE = 'activate_account';
const OPTION_NAME = 'tz_auth'; // Database lookup key (`wp_options`.`option_name`)
call_user_func(function() {
Vars::$options = new Tools\WP_Option(OPTION_NAME);
if (is_admin()) {
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'Settings.php');
}
if (is_array(Vars::$options['third_party'])) {
foreach (Vars::$options['third_party'] as $tp => $on) {
if ($on) {
require_once(__DIR__ . DIRECTORY_SEPARATOR . $tp . DIRECTORY_SEPARATOR . $tp . '.php');
}
}
}
});
/**
* Attempts to login the user
* @param {String} $username
......@@ -80,7 +100,11 @@ function forgot_password($username, $forgot_method) {
}
class Validation extends \Tz\Validation {
class Vars {
public static $options;
}
class Validation extends Common\Validation {
/**
* @rule Not blank
* @rule Valid WordPress username
......
<?php
/**
* This needs to go in the <html tag
* xmlns:fb="http://www.facebook.com/2008/fbml"
*
* http://wpdev.tenzinghost.com
* API Key: 83f54e078b9aa0e303bba959dc0a566f
* App Secret: e542aca35ab698121fa5917211013a41
* App ID: 105917066126941
*
* http://wp.cb
* API Key: 3bcccfd8c28c52197141266d9e417649
* App Secret: 9bfcd828bc6ccef12336dea57df93ecb
* App ID: 138943536118944
*/
namespace Tz\WordPress\Tools\Auth\Facebook;
use Tz\WordPress\Tools;
use Tz\WordPress\Tools\ShortCodes as SC;
use InvalidArgumentException;
const OPTION_NAME = 'tz_auth_fb';
call_user_func(function() {
SC\add_shortcodes(__NAMESPACE__ . '\ShortCodes');
Vars::$options = new Tools\WP_Option(OPTION_NAME);
if (is_admin()) {
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'Settings.php');
}
});
function loadJSSDK() {
return '
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({appId: \'' . Vars::$options['application_id'] . '\', status: true, cookie: true, xfbml: true});
FB.Event.subscribe(\'auth.login\', function(response) { window.location.reload(); });
};
(function() {
var e = document.createElement(\'script\'); e.async = true;
e.src = document.location.protocol + \'//connect.facebook.net/en_US/all.js\';
document.getElementById(\'fb-root\').appendChild(e);
}());
</script>
';
}
function drawLoginButton($value = 'Login') {
echo '<fb:login-button></fb:login-button>';
// echo '<a class="fb_button fb_button_medium"><span class="fb_button_text">' . $value . '</span></a>';
}
class Vars {
public static $options = false;
}
class ShortCodes {
public static function fb_login_button() {
ob_start();
drawLoginButton();
$btn = ob_get_contents();
ob_end_clean();
return $btn;
}
}
?>
\ No newline at end of file
<?php
// todo: move ext_perms to a constant somewhere
namespace Tz\WordPress\Tools\Auth\Facebook\Settings;
use Tz\WordPress\Tools, Tz\WordPress\Tools\Auth, Tz\WordPress\Tools\Auth\Facebook;
const OPTION_SECTION_CRED = 'fb_creds';
const OPTION_SECTION_SEL = 'fb_opts';
call_user_func(function() {
Vars::$options = new Tools\WP_Option(Facebook\OPTION_NAME);
Tools\add_actions(__NAMESPACE__ . '\Actions');
});
function validate($data) {
return (array)$data;
}
class Vars {
public static $options = false;
public static $data_permissions = Array('email', 'read_insights', 'read_stream', 'read_mailbox', 'ads_management', 'xmpp_login', 'user_about_me', 'user_activities', 'user_birthday', 'user_education_history', 'user_events', 'user_groups', 'user_hometown', 'user_interests', 'user_likes', 'user_location', 'user_notes', 'user_online_presence', 'user_photo_video_tags', 'user_photos', 'user_relationships', 'user_religion_politics', 'user_status', 'user_videos', 'user_website', 'user_work_history', 'read_friendlists', 'read_requests');
}
class Actions {
public static function admin_init() {
register_setting(Auth\Settings\OPTION_GROUP, Facebook\OPTION_NAME, __NAMESPACE__ . '\validate');
add_settings_section(OPTION_SECTION_CRED, 'Facebook Credentials', function(){}, Auth\Settings\ADMIN_PAGE);
Tools\add_settings_fields(__NAMESPACE__ . '\Cred_Fields', Auth\Settings\ADMIN_PAGE, OPTION_SECTION_CRED);
add_settings_section(OPTION_SECTION_SEL, 'Facebook Extended Data Permissions', function() { echo '<p>Select which additional data you wish to collect from the user</p>'; }, Auth\Settings\ADMIN_PAGE);
foreach (Vars::$data_permissions as $option) {
add_settings_field($option, ucwords(str_replace('_', ' ', $option)), Array(new Opt_Fields(), $option), Auth\Settings\ADMIN_PAGE, OPTION_SECTION_SEL);
}
}
}
class Cred_Fields {
public static function api_key() {
echo '<input type="text" id="' . __FUNCTION__ . '" name="' . Facebook\OPTION_NAME . '[' . __FUNCTION__ . ']" value="' . Vars::$options[__FUNCTION__] . '" size="45" />';
}
public static function application_secret() {
echo '<input type="text" id="' . __FUNCTION__ . '" name="' . Facebook\OPTION_NAME . '[' . __FUNCTION__ . ']" value="' . Vars::$options[__FUNCTION__] . '" size="45" />';
}
public static function application_id() {
echo '<input type="text" id="' . __FUNCTION__ . '" name="' . Facebook\OPTION_NAME . '[' . __FUNCTION__ . ']" value="' . Vars::$options[__FUNCTION__] . '" />';
}
}
class Opt_Fields {
public function __call($fn, $args) {
echo '<input type="checkbox" id="' . $fn . '" name="' . Facebook\OPTION_NAME . '[ext_perms][' . $fn . ']" value="1" ' . checked('1', Vars::$options['ext_perms'][$fn], false) . ' />';
}
}
?>
\ No newline at end of file
<?php
namespace Tz\WordPress\Tools\Auth\Settings;
use Tz\Common;
use Tz\WordPress\Tools;
use Tz\WordPress\Tools\Auth;
const OPTION_GROUP = 'auth_group'; // Grouping of options used for setting output by WP
const OPTION_SECTION = 'tz_auth_main';
const ADMIN_PAGE = 'tz-tools-auth'; // URI of options page
const CAPABILITY = 'manage_auth'; // User & Roles capability name
call_user_func(function() {
$role = get_role('administrator');
$role->add_cap(CAPABILITY);
Tools\add_actions(__NAMESPACE__ . '\Actions');
});
function displayPage() {
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'settings_view.php');
}
function validate($data) {
return $data;
// Validation library causing some issues with Array
$valid = new Validation((array)$data);
return $valid->valid;
}
class Actions {
public static function admin_menu() {
add_options_page('Authentication', 'Authentication', CAPABILITY, ADMIN_PAGE, __NAMESPACE__ . '\displayPage');
}
public static function admin_init() {
register_setting(OPTION_GROUP, Auth\OPTION_NAME, __NAMESPACE__ . '\validate');
add_settings_section(OPTION_SECTION, 'Authentication Pages', function(){}, ADMIN_PAGE);
Tools\add_settings_fields(__NAMESPACE__ . '\Fields', ADMIN_PAGE, OPTION_SECTION);
}
}
class Fields {
public static function login_page() {
_dropdown_pages(Array('name' => Auth\OPTION_NAME . '[' . __FUNCTION__ . ']', 'sort_column' => 'menu_order', 'echo' => 1, 'selected' => Auth\Vars::$options[__FUNCTION__]));
}
public static function account_page() {
_dropdown_pages(Array('name' => Auth\OPTION_NAME . '[' . __FUNCTION__ . ']', 'sort_column' => 'menu_order', 'echo' => 1, 'selected' => Auth\Vars::$options[__FUNCTION__]));
}
public static function Facebook() {
echo '<input type="checkbox" id="' . __FUNCTION__ . '" name="' . Auth\OPTION_NAME . '[third_party][' . __FUNCTION__ . ']" value="1" ' . checked('1', Auth\Vars::$options['third_party'][__FUNCTION__], false) . ' />';
}
}
class Validation extends Common\Validation {
public static function login_page($val) {
// if !is page throw exception
}
public static function account_page($val) {
// if !is page throw exception
// if is same as login_page throw exception
}
}
?>
\ No newline at end of file
<?php
namespace Tz\WordPress\Tools\Auth\Settings;
?>
<div class="wrap">
<?php screen_icon(); ?>
<h2>Authentication Settings</h2>
<form method="post" action="options.php">
<?php
settings_fields(OPTION_GROUP);
do_settings_sections(ADMIN_PAGE);
?>
<p class="submit"><input type="submit" class="button-primary" value="Save Changes" /></p>
</form>
</div>
\ No newline at end of file