Appmaker: Home Page "AppStart" upon user role - google-app-maker

I want the Admin's start page to be the Admin Console and for the user another page - "MyTickets". The default start page for the app is MyTickets but I don't want the Admin to see this as the start page nor be able to navigate to it.
I found in the Appmaker's templates "Training hub" some code for the Appstart depending on the user role, as I want; However, when I implemented it, I get this error in the console
The page keeps loading while in the template it works normally.
Here is the code I inserted in my app in client script:
/**
* Determines whether the user has specified role.
* #param {string} roleName - name of the role to check.
* #return {boolean} true if user has the role.
*/
function hasRole(roleName) {
return (app.user.roles.indexOf(roleName) > -1);
}
/**
* Determines whether the user is admin.
* #return {boolean} true if user is an admin.
*/
function isAdmin() {
return hasRole('Admins');
}
/**
* Gets start page depends on current user's roles.
* #return {Page} start page to be shown to user.
*/
function getUserStartPage() {
var result = app.pages.MyTickets;
var userRoles = app.user.roles;
if (isAdmin()) {
result = app.pages.Admin_console;
}
return result;
}
/**
* Overrides start page if loading is not specified.
* #param {string} currentPageName - name of current page to be loaded.
* #return {Page} start page to be shown to user.
*/
function overrideStartPage(currentPageName) {
if (currentPageName) {
return;
}
var startPage = getUserStartPage();
gotoPage(startPage);
}
/**
* Handles Application Start event.
* Loads Application Settings and then loads the app.
* #param {AppLoader} loader - instance of application loader.
*/
function onAppStart(loader) {
loader.suspendLoad();
google.script.url.getLocation(function(location) {
app.datasources.AppSettings.load({
success: function() {
overrideStartPage(location.hash);
loader.resumeLoad();
},
failure: function() {
overrideStartPage(location.hash);
loader.resumeLoad();
}
});
});
}
And in the app's settings, the startup script is:
onAppStart(loader);

I'd suggest you to dig a bit more into javascript or into the app maker documentation. The solution is very easy but since you are copying and pasting the source code without understanding what is happening, you are having difficulties resolving this matter. Here is the code you need to use:
loader.suspendLoad();
if(app.user.role.Admins){
app.showPage(app.pages.Admin_console);
} else {
app.showPage(app.pages.MyTickets);
}
loader.resumeLoad();
Note Bene: The above code should be used exactly as it is inside the App Settings - App startup script
References: https://developers-dot-devsite-v2-prod.appspot.com/appmaker/scripting/api/client#App

Related

Start session when page is loaded in WordPress

I want to create a session at the first visit on a page. I can do it, when I'm logged in. So the session get's created when I log in. When i log out, then a new session get's created.
I added to functions.php following code:
/**
* Start Session
*/
function start_session() {
if(session_id) {
session_start();
}
}
add_action('init', 'start_session');
/**
* End Session
*/
function end_session() {
session_destroy ();
}
add_action('wp_logout','end_session');
add_action('wp_login','end_session');

Re-init application laravel

I have two websites in the one laravel app. Http requests have no problems: depending of domain app include different views, config and other. But commands and async jobs have problems. Default app create. I transmit parameter (domain), but can't re-init app. I do anything, but pathes are default.
$app = require DIR.'/../bootstrap/app.php'; $app->make(Kernel::class)->bootstrap();
I tried, but it not helped.
Tell me please how do it.
I hope that it will be helpful for anybody. I wrote this helper:
/**
* Init the application.
*
* #param string $domain
* #return void
*/
function initApplication(string $domain = App::DOMAINS[App::DEFAULT_SUBDIR])
{
if (function_exists('putenv')) {
array_map('putenv', array_keys($_ENV));
}
$_SERVER = array_diff_key($_SERVER, $_ENV);
$_ENV = [];
global $app;
$_SERVER['SERVER_NAME'] = $domain;
$app = require __DIR__.'/../bootstrap/app.php';
(new Dotenv\Loader($app->environmentFilePath()))->load();
$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
}

behat 3 mink testing redirection

In symfony 3 controller file I have function:
/**
* #Route("/user/registration", name="post_registration")
* #Method("POST")
* #return mixed
*/
public function postRegistration()
{
$post = $this->getAllPost();
$this->curl = $this->get('ApiClient');
$responseArray = $this->curl->post(
$this->container->getParameter('api_url') . '/users',
$post
);
if (isset($responseArray['api_key'])) {
return $this->redirectResponseWithCookie($responseArray['api_key']);
} else {
return $this->render(
static::REGISTRATION_TEMPLATE,
['errors' => $responseArray['errors']]
);
}
}
In one part it calls function
redirectResponseWithCookie()
which should redirect the page.
I want to test redirection header - does it have the right Location value.
I have function in UserRegistrationContext class
class UserRegistrationContext extends BaseContext implements Context, SnippetAcceptingContext
{
/**
* #When I register successfully into the system
* #return null
*/
public function iRegisterSuccessfullyIntoTheSystem()
{
$this->canIntercept();
$this->session->getDriver()->getClient()->followRedirects(false);
// Enters his data
// Posts to www.notification.guru .
$this->session->getDriver()->getClient()
->request('POST', $this->baseUrl . '/user/registration', $this->getFormArray());
echo 'test';
}
}
BaseContext class just has some helper functions, its contructor inits session:
$driver = new GoutteDriver();
$this->session = new Session($driver);
this part might be wrong:
$this->session->getDriver()->getClient()->followRedirects(false);
I just took code from behat 2.5 and tried to adjust to work it with behat 3, but not sure if its correct, but at least does not throw error.
It should stop redirect, so then I could get a response header with
getResponseHeaders()
But the problem is that it tries to redirect, and code fails, because real site is not lauched yet where it redirects. And also I would not be able to test headers I guess after real redirection.
So the redirection has to be stopped I guess.
How to do that? I cannot find info.
Test fails at line
$this->session->getDriver()->getClient()
->request('POST', $this->baseUrl . '/user/registration', $this->getFormArray());
So code in my question is not wrong, as I said in comment - I did not see well in console. Often happens that when I post to SO, I do not even finish writing the question, just from writing the details to others I see the answer.
$this->session->getDriver()->getClient()->followRedirects(false);
works well.
So to finish how to check - make function something like this and call it:
/**
* #param Behat\Mink\Session $session - mink session
* #throws \Exception
* #return null
*/
public function isLoggedIntoApi($session)
{
$headers = $session->getResponseHeaders();
if ($headers['Location'][0] != $this->appUrl) {
throw new \Exception(
'User is not redirected to ' . $this->appUrl
);
}
}

Migrations fail when SQLite database file does not exist?

It seems that migrations (sort of) fail silently when the database file does not exist. The migration executes but no db file is created and I can run the migration again. (It never says "nothing to migrate") If I create a blank file then it works.
This is odd because I thought SQLite always created the db file if it was not found so I'm not sure if this is a bug or something I've done wrong. Maybe it's a permissions problem? But everything else is working so I don't know. I'm using Windows 7 and the project is in my
User blamh suggested to add the following snippet to app/start/artisan.php to automatically recreate the database when it doesn't exist, instead of throwing an exception.
if (Config::get('database.default') === 'sqlite') {
$path = Config::get('database.connections.sqlite.database');
if (!file_exists($path) && is_dir(dirname($path))) {
touch($path);
}
}
With this, you can safely delete the SQLite database and then re-migrate and re-seed it, if you wish.
I've issued this bug against laravel/framework.
Hopefully future versions will give an error if the database doesn't exist, or automatically create one.
This is an updated and more flexible solution from Virtlinks answer
<?php
namespace App\Providers;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\ServiceProvider;
class SqliteServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
if (DB::getDriverName() === 'sqlite') {
$path = DB::getConfig('database');
if (!file_exists($path) && is_dir(dirname($path))) {
touch($path);
}
}
}
/**
* Register any application services.
*
* #return void
*/
public function register()
{
//
}
}
Here's yet another way to automatically create the database file, tested on Laravel 5.4.
This is the same as Gummibeer's answer except that I moved the logic to the App\Console\Kernel class (app/Console/Kernel.php), and the check will be performed only when running the migrate command.
<?php
use Illuminate\Support\Facades\DB;
class Kernel extends ConsoleKernel
{
/**
* #param \Symfony\Component\Console\Input\InputInterface $input
* #param \Symfony\Component\Console\Output\OutputInterface $output
* #return int
*/
public function handle($input, $output = null)
{
$this->touchSQLiteDatabase($input);
return parent::handle($input, $output);
}
protected function touchSQLiteDatabase($input)
{
$this->bootstrap();
if (substr((string)$input, 0, 7) == 'migrate' && DB::getDriverName() === 'sqlite') {
$path = DB::getConfig('database');
if (!file_exists($path) && is_dir(dirname($path))) {
touch($path);
}
}
}
}

How to modify FOSUserBundle unique validator?

I'm trying to modify the way FOSUSerBundle validates the username/email when the user registers, because I need to check if the user was previously registered, but he has unsubscribed, through status flag in the entity. I think the best way is check if email is previously on the database, excluding those who the flag unsubscribed is set to true, but how I do this?
So, I see how validates with UniqueValidator class, but I don't understand how it works. It would be great if anyone could explain it too.
Thanks.
UniqueValidator calls validateUnique method of UserManager class. You can extend the class and modify the function to check user
$this->findUserByEmail($user->getEmail());
Edit
For second question, you have to override UpdateUser method of the UserManager class.
/**
* Updates a user.
*
* #param UserInterface $user
* #param Boolean $andFlush Whether to flush the changes (default true)
*/
public function updateUser(UserInterface $user, $andFlush = true)
{
$existsUser = $this->findUserByEmail($user->getEmail());
$this->updateCanonicalFields($user);
$this->updatePassword($user);
if($existsUser && null === $user->getId()){
$user->setId($existsUser->getId());
$this->em->merge($user);
} else{
$this->em->persist($user);
}
if ($andFlush) {
$this->em->flush();
}
}

Resources