winix/winixd/templates/env.cpp

298 lines
5.1 KiB
C++

/*
* This file is a part of Winix
* and is distributed under the 2-Clause BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2012-2021, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "templates.h"
#include "core/misc.h"
#include "functions/functions.h"
#include "miscspace.h"
namespace Winix
{
namespace TemplatesFunctions
{
void env_str(Info & i)
{
User * puser = cur->session->puser;
if( puser )
puser->env.serialize_to_space_stream(i.out, true);
}
void env(Info & i)
{
User * puser = cur->session->puser;
if( puser )
space_value(i, puser->env);
}
void env_tab(Info & i)
{
User * puser = cur->session->puser;
if( puser )
space_list_tab(i, puser->env);
}
void env_tab_value(Info & i)
{
User * puser = cur->session->puser;
if( puser )
space_list_tab_value(i, puser->env, L"env_tab");
}
void env_tab_has_next(Info & i)
{
User * puser = cur->session->puser;
if( puser )
space_list_tab_has_next(i, puser->env, L"env_tab");
}
void env_admin_str(Info & i)
{
User * puser = cur->session->puser;
if( puser )
puser->aenv.serialize_to_space_stream(i.out, true);
}
void env_admin(Info & i)
{
User * puser = cur->session->puser;
if( puser )
space_value(i, puser->aenv);
}
void env_admin_tab(Info & i)
{
User * puser = cur->session->puser;
if( puser )
space_list_tab(i, puser->aenv);
}
void env_admin_tab_value(Info & i)
{
User * puser = cur->session->puser;
if( puser )
space_list_tab_value(i, puser->aenv, L"env_admin_tab");
}
void env_admin_tab_has_next(Info & i)
{
User * puser = cur->session->puser;
if( puser )
space_list_tab_has_next(i, puser->aenv, L"env_admin_tab");
}
static size_t req_id = 0;
static User * puser = nullptr;
/*
* IMPROVEME
* in the future the user pointer will be set by the env controller
* a new struct will be added and put to templates (when new ezc object templates will be ready)
*
*/
User * env_get_user()
{
if( cur->request->id != req_id )
{
req_id = cur->request->id;
puser = 0;
if( cur->session->puser )
{
if( cur->session->puser->super_user && cur->request->IsPostVar(L"userid") )
{
long id = Tol(cur->request->PostVar(L"userid"));
puser = system->users.GetUser(id);
}
else
{
puser = cur->session->puser;
}
}
}
return puser;
}
void env_user_admin_env_str(Info & i)
{
// only an admin is able to see this variables
if( cur->session->puser && cur->session->puser->super_user )
{
User * puser = env_get_user();
if( puser )
i.out << puser->aenv;
}
}
void env_user_env_str(Info & i)
{
User * puser = env_get_user();
if( puser )
i.out << puser->env;
}
void env_user_id(Info & i)
{
User * puser = env_get_user();
if( puser )
i.out << puser->id;
}
void env_user_name(Info & i)
{
User * puser = env_get_user();
if( puser )
i.out << puser->name;
}
static Users::Iterator user_iter;
static size_t user_reqid = 0;
static size_t user_index; // only information
bool env_user_tab_init()
{
if( user_reqid != cur->request->id )
{
user_reqid = cur->request->id;
user_iter = system->users.End();
}
return user_iter != system->users.End();
}
void env_user_tab(Info & i)
{
env_user_tab_init();
if( cur->session->puser && cur->session->puser->super_user )
{
user_index = i.iter;
if( i.iter == 0 )
user_iter = system->users.Begin();
else
if( user_iter != system->users.End() )
++user_iter;
i.res = user_iter != system->users.End();
}
}
void env_user_tab_id(Info & i)
{
if( env_user_tab_init() )
i.out << user_iter->id;
}
void env_user_tab_name(Info & i)
{
if( env_user_tab_init() )
i.out << user_iter->name;
}
void env_user_tab_is_current(Info & i)
{
if( env_user_tab_init() )
{
User * puser = env_get_user();
if( puser )
i.res = (user_iter->id == puser->id );
}
}
} // namespace TemplatesFunctions
} // namespace Winix