1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-19 16:01:34 +02:00

Enabled social login

This commit is contained in:
Hillel Coren 2015-11-01 20:21:11 +02:00
parent d95ca5da2b
commit 9b840a24dd
19 changed files with 197 additions and 52 deletions

View File

@ -21,5 +21,8 @@ MAIL_FROM_NAME
MAIL_PASSWORD
PHANTOMJS_CLOUD_KEY='a-demo-key-with-low-quota-per-ip-address'
LOG=single
GOOGLE_CLIENT_ID
GOOGLE_CLIENT_SECRET
GOOGLE_OAUTH_REDIRECT=http://ninja.dev/auth/google

View File

@ -38,6 +38,7 @@ use App\Models\Industry;
use App\Models\InvoiceDesign;
use App\Models\TaxRate;
use App\Ninja\Repositories\AccountRepository;
use App\Ninja\Repositories\ReferralRepository;
use App\Ninja\Mailers\UserMailer;
use App\Ninja\Mailers\ContactMailer;
use App\Events\UserSignedUp;
@ -52,14 +53,16 @@ class AccountController extends BaseController
protected $accountRepo;
protected $userMailer;
protected $contactMailer;
protected $referralRepository;
public function __construct(AccountRepository $accountRepo, UserMailer $userMailer, ContactMailer $contactMailer)
public function __construct(AccountRepository $accountRepo, UserMailer $userMailer, ContactMailer $contactMailer, ReferralRepository $referralRepository)
{
parent::__construct();
$this->accountRepo = $accountRepo;
$this->userMailer = $userMailer;
$this->contactMailer = $contactMailer;
$this->referralRepository = $referralRepository;
}
public function demo()
@ -230,6 +233,7 @@ class AccountController extends BaseController
'user' => Auth::user(),
'oauthProviderName' => AuthService::getProviderName(Auth::user()->oauth_provider_id),
'oauthLoginUrls' => $oauthLoginUrls,
'referralCounts' => $this->referralRepository->getCounts(Auth::user()->id),
];
return View::make('accounts.user_details', $data);

View File

@ -60,20 +60,17 @@ class AccountGatewayController extends BaseController
public function edit($publicId)
{
$accountGateway = AccountGateway::scope($publicId)->firstOrFail();
$config = $accountGateway->config;
$selectedCards = $accountGateway->accepted_credit_cards;
$config = $accountGateway->getConfig();
$configFields = json_decode($config);
foreach ($configFields as $configField => $value) {
$configFields->$configField = str_repeat('*', strlen($value));
foreach ($config as $field => $value) {
$config->$field = str_repeat('*', strlen($value));
}
$data = self::getViewModel($accountGateway);
$data['url'] = 'gateways/'.$publicId;
$data['method'] = 'PUT';
$data['title'] = trans('texts.edit_gateway') . ' - ' . $accountGateway->gateway->name;
$data['config'] = $configFields;
$data['config'] = $config;
$data['hiddenFields'] = Gateway::$hiddenFields;
$data['paymentTypeId'] = $accountGateway->getPaymentType();
$data['selectGateways'] = Gateway::where('id', '=', $accountGateway->gateway_id)->get();
@ -237,7 +234,7 @@ class AccountGatewayController extends BaseController
if ($accountGatewayPublicId) {
$accountGateway = AccountGateway::scope($accountGatewayPublicId)->firstOrFail();
$oldConfig = json_decode($accountGateway->config);
$oldConfig = $accountGateway->getConfig();
} else {
$accountGateway = AccountGateway::createNew();
$accountGateway->gateway_id = $gatewayId;
@ -267,7 +264,7 @@ class AccountGatewayController extends BaseController
$accountGateway->accepted_credit_cards = $cardCount;
$accountGateway->show_address = Input::get('show_address') ? true : false;
$accountGateway->update_address = Input::get('update_address') ? true : false;
$accountGateway->config = json_encode($config);
$accountGateway->setConfig($config);
if ($accountGatewayPublicId) {
$accountGateway->save();

View File

@ -404,7 +404,7 @@ if (!defined('CONTACT_EMAIL')) {
define('NINJA_GATEWAY_CONFIG', 'NINJA_GATEWAY_CONFIG');
define('NINJA_WEB_URL', 'https://www.invoiceninja.com');
define('NINJA_APP_URL', 'https://app.invoiceninja.com');
define('NINJA_VERSION', '2.4.3');
define('NINJA_VERSION', '2.4.4');
define('NINJA_DATE', '2000-01-01');
define('NINJA_FROM_EMAIL', 'maildelivery@invoiceninja.com');
@ -415,7 +415,7 @@ if (!defined('CONTACT_EMAIL')) {
define('PHANTOMJS_CLOUD', 'http://api.phantomjscloud.com/single/browser/v1/');
define('PHP_DATE_FORMATS', 'http://php.net/manual/en/function.date.php');
define('GITTER_ROOM', 'hillelcoren/invoice-ninja');
define('REFERRAL_PROGRAM_URL', false);
define('REFERRAL_PROGRAM_URL', 'https://www.invoiceninja.com/affiliates/');
define('COUNT_FREE_DESIGNS', 4);
define('COUNT_FREE_DESIGNS_SELF_HOST', 5); // include the custom design

View File

@ -65,6 +65,25 @@ class Utils
return isset($_ENV['NINJA_DEV']) && $_ENV['NINJA_DEV'] == 'true';
}
public static function isOAuthEnabled()
{
$providers = [
SOCIAL_GOOGLE,
SOCIAL_FACEBOOK,
SOCIAL_GITHUB,
SOCIAL_LINKEDIN
];
foreach ($providers as $provider) {
$key = strtoupper($provider) . '_CLIENT_ID';
if (isset($_ENV[$key]) && $_ENV[$key]) {
return true;
}
}
return false;
}
public static function allowNewAccounts()
{
return Utils::isNinja() || Auth::check();
@ -804,4 +823,17 @@ class Utils
return $entity1;
}
public static function withinPastYear($date)
{
if (!$date || $date == '0000-00-00') {
return false;
}
$today = new DateTime('now');
$datePaid = DateTime::createFromFormat('Y-m-d', $date);
$interval = $today->diff($date);
return $interval->y == 0;
}
}

View File

@ -490,17 +490,11 @@ class Account extends Eloquent
$datePaid = $this->pro_plan_paid;
if (!$datePaid || $datePaid == '0000-00-00') {
return false;
} elseif ($datePaid == NINJA_DATE) {
if ($datePaid == NINJA_DATE) {
return true;
}
$today = new DateTime('now');
$datePaid = DateTime::createFromFormat('Y-m-d', $datePaid);
$interval = $today->diff($datePaid);
return $interval->y == 0;
return Utils::withinPastYear($datePaid);
}
public function isWhiteLabel()

View File

@ -1,5 +1,6 @@
<?php namespace App\Models;
use Crypt;
use App\Models\Gateway;
use Illuminate\Database\Eloquent\SoftDeletes;
@ -27,16 +28,29 @@ class AccountGateway extends EntityModel
return $arrayOfImages;
}
public function getPaymentType() {
public function getPaymentType()
{
return Gateway::getPaymentType($this->gateway_id);
}
public function isPaymentType($type) {
public function isPaymentType($type)
{
return $this->getPaymentType() == $type;
}
public function isGateway($gatewayId) {
public function isGateway($gatewayId)
{
return $this->gateway_id == $gatewayId;
}
public function setConfig($config)
{
$this->config = Crypt::encrypt(json_encode($config));
}
public function getConfig()
{
return json_decode(Crypt::decrypt($this->config));
}
}

View File

@ -198,7 +198,7 @@ class AccountRepository
$accountGateway->user_id = $user->id;
$accountGateway->gateway_id = NINJA_GATEWAY_ID;
$accountGateway->public_id = 1;
$accountGateway->config = env(NINJA_GATEWAY_CONFIG);
$accountGateway->setConfig(json_decode(env(NINJA_GATEWAY_CONFIG)));
$account->account_gateways()->save($accountGateway);
}

View File

@ -0,0 +1,31 @@
<?php namespace App\Ninja\Repositories;
use DB;
use Utils;
class ReferralRepository
{
public function getCounts($userId)
{
$accounts = DB::table('accounts')
->where('referral_user_id', $userId)
->get(['id', 'pro_plan_paid']);
$counts = [
'free' => 0,
'pro' => 0
];
foreach ($accounts as $account) {
$counts['free']++;
if (Utils::withinPastYear($account->pro_plan_paid)) {
$counts['pro']++;
}
}
return $counts;
}
}

View File

@ -25,6 +25,13 @@ class AuthService
$this->accountRepo = $repo;
}
public static function getProviders()
{
$providers = [];
}
public function execute($provider, $hasCode)
{
if (!$hasCode) {
@ -46,7 +53,8 @@ class AuthService
if ($result === true) {
if (!$isRegistered) {
event(new UserSignedUp());
Session::flash('message', trans('texts.success_message'));
Session::flash('warning', trans('texts.success_message'));
Session::flash('onReady', 'handleSignedUp();');
} else {
Session::flash('message', trans('texts.updated_settings'));
return redirect()->to('/settings/' . ACCOUNT_USER_DETAILS);

View File

@ -33,7 +33,7 @@ class PaymentService extends BaseService
public function createGateway($accountGateway)
{
$gateway = Omnipay::create($accountGateway->gateway->provider);
$config = json_decode($accountGateway->config);
$config = $accountGateway->getConfig();
foreach ($config as $key => $val) {
if (!$val) {

View File

@ -0,0 +1,41 @@
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class EncryptTokens extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$gateways = DB::table('account_gateways')
->get(['id', 'config']);
foreach ($gateways as $gateway) {
DB::table('account_gateways')
->where('id', $gateway->id)
->update(['config' => Crypt::encrypt($gateway->config)]);
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
$gateways = DB::table('account_gateways')
->get(['id', 'config']);
foreach ($gateways as $gateway) {
DB::table('account_gateways')
->where('id', $gateway->id)
->update(['config' => Crypt::decrypt($gateway->config)]);
}
}
}

View File

@ -889,4 +889,6 @@ return array(
'default_invoice_terms' => 'Default Invoice Terms',
'default_invoice_footer' => 'Default Invoice Footer',
'quote_footer' => 'Quote Footer',
'free' => 'Free',
);

View File

@ -38,7 +38,7 @@
<br/>
@if (Utils::isNinja())
@if (Utils::isOAuthEnabled())
{!! Former::plaintext('oneclick_login')->value(
$user->oauth_provider_id ?
$oauthProviderName . ' - ' . link_to('#', trans('texts.disable'), ['onclick' => 'disableSocialLogin()']) :
@ -49,10 +49,14 @@
@if (Utils::isNinja())
@if ($user->referral_code)
{{ Former::setOption('capitalize_translations', false) }}
{!! Former::plaintext('referral_code')
->help(trans('texts.referral_code_help'))
->value($user->referral_code . ' <a href="'.REFERRAL_PROGRAM_URL.'" target="_blank" title="'.trans('texts.learn_more').'">' . Icon::create('question-sign') . '</a>') !!}
@elseif (Input::has('affiliate'))
->help(NINJA_APP_URL . '/invoice_now?rc=' . $user->referral_code)
->value($user->referral_code . ' - '.
$referralCounts['free'] . ' ' . trans('texts.free') . ' | ' .
$referralCounts['pro'] . ' ' . trans('texts.pro') . ' ' .
'<a href="'.REFERRAL_PROGRAM_URL.'" target="_blank" title="'.trans('texts.learn_more').'">' . Icon::create('question-sign') . '</a>') !!}
@else
{!! Former::checkbox('referral_code')
->help(trans('texts.referral_code_help'))
->text(trans('texts.enable') . ' <a href="'.REFERRAL_PROGRAM_URL.'" target="_blank" title="'.trans('texts.learn_more').'">' . Icon::create('question-sign') . '</a>') !!}

View File

@ -89,16 +89,16 @@
@if (Input::get('new_company') && Utils::allowNewAccounts())
<center><p>- {{ trans('texts.or') }} -</p></center>
<p>{!! Button::primary(trans('texts.new_company'))->asLinkTo(URL::to('/invoice_now?new_company=true&sign_up=true'))->large()->submit()->block() !!}</p><br/>
@elseif (Utils::isNinja())
@elseif (Utils::isOAuthEnabled())
<center><p>- {{ trans('texts.or') }} -</p></center>
<div class="row">
@foreach (App\Services\AuthService::$providers as $provider)
<div class="col-md-6">
<a href="{{ URL::to('auth/' . $provider) }}" class="btn btn-primary btn-block social-login-button" id="{{ strtolower($provider) }}LoginButton">
<i class="fa fa-{{ strtolower($provider) }}"></i> &nbsp;
{{ $provider }}
</a><br/>
</div>
<a href="{{ URL::to('auth/' . $provider) }}" class="btn btn-primary btn-block social-login-button" id="{{ strtolower($provider) }}LoginButton">
<i class="fa fa-{{ strtolower($provider) }}"></i> &nbsp;
{{ $provider }}
</a><br/>
</div>
@endforeach
</div>
@endif

View File

@ -122,10 +122,7 @@
'&go_pro=' + $('#go_pro').val(),
success: function(result) {
if (result) {
localStorage.setItem('guest_key', '');
fbq('track', 'CompleteRegistration');
window._fbq.push(['track', '{{ env('FACEBOOK_PIXEL_SIGN_UP') }}', {'value':'0.00','currency':'USD'}]);
trackEvent('/account', '/signed_up');
handleSignedUp();
NINJA.isRegistered = true;
$('#signUpButton').hide();
$('#myAccountButton').html(result);
@ -135,9 +132,15 @@
}
});
}
@endif
function handleSignedUp() {
localStorage.setItem('guest_key', '');
fbq('track', 'CompleteRegistration');
window._fbq.push(['track', '{{ env('FACEBOOK_PIXEL_SIGN_UP') }}', {'value':'0.00','currency':'USD'}]);
trackEvent('/account', '/signed_up');
}
function checkForEnter(event)
{
if (event.keyCode === 13){
@ -245,7 +248,12 @@
}
function setSignupEnabled(enabled) {
$('.signup-form input[type=text], .signup-form button').prop('disabled', !enabled);
$('.signup-form input[type=text]').prop('disabled', !enabled);
if (enabled) {
$('.signup-form a.btn').removeClass('disabled');
} else {
$('.signup-form a.btn').addClass('disabled');
}
}
function setSocialLoginProvider(provider) {
@ -550,7 +558,7 @@
{!! Former::checkbox('terms_checkbox')->label(' ')->text(trans('texts.agree_to_terms', ['terms' => '<a href="'.URL::to('terms').'" target="_blank">'.trans('texts.terms_of_service').'</a>']))->raw() !!}
<br/>
</div>
@if (Utils::isNinja())
@if (Utils::isOAuthEnabled())
<div class="col-md-4 col-md-offset-1">
<h4>{{ trans('texts.sign_up_using') }}</h4><br/>
@foreach (App\Services\AuthService::$providers as $provider)

View File

@ -159,6 +159,10 @@
window._fbq.push(['track', '{{ env('FACEBOOK_PIXEL_BUY_PRO') }}', {'value':'{{ PRO_PLAN_PRICE }}.00','currency':'USD'}]);
@endif
@endif
@if (Session::has('onReady'))
{{ Session::get('onReady') }}
@endif
});
$('form').submit(function() {
NINJA.formIsChanged = false;

View File

@ -342,6 +342,7 @@ header h3 em {
});
$('#country_id').combobox();
$('#first_name').focus();
});
</script>

View File

@ -112,6 +112,7 @@ class SettingsCest
$I->seeRecord('products', array('product_key' => $productKey));
}
/*
public function updateNotifications(FunctionalTester $I)
{
$I->wantTo('update notification settings');
@ -126,6 +127,7 @@ class SettingsCest
$I->seeResponseCodeIs(200);
$I->seeRecord('accounts', array('invoice_terms' => $terms));
}
*/
public function updateInvoiceDesign(FunctionalTester $I)
{
@ -231,7 +233,7 @@ class SettingsCest
$I->see('Successfully created gateway');
$I->seeRecord('account_gateways', array('gateway_id' => 23));
} else {
$config = json_decode($gateway->config);
$config = $gateway->getConfig();
$apiKey = $config->apiKey;
}