winix/winixd/models/user.cpp

197 lines
4.2 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 "user.h"
#include "core/misc.h"
#include "templates/templates.h"
namespace Winix
{
User::User()
{
Clear();
}
void User::fields()
{
field(L"id", id, morm::FT::no_insertable | morm::FT::no_updatable | morm::FT::primary_key);
field(L"login", name); // IMPROVEME set the same name, either 'login' or 'name'
field(L"super_user", super_user); // IMPROVEME rename to is_super_user
field(L"has_pass", has_pass);
field(L"pass_type", pass_type);
field(L"password", L"", password);
field(L"pass_encrypted", L"", pass_encrypted, morm::FT::binary);
field(L"pass_hash_salted", pass_hash_salted); // IMPROVEME rename to pass_is_hash_salted or is_pass_hash_salted
field(L"email", email);
field(L"notify", notify);
field(L"env", env);
field(L"aenv", aenv); // IMPROVEME rename to admin_env
field(L"status", status);
field(L"locale_id", locale_id);
field(L"time_zone_id", time_zone_id);
}
void User::table()
{
table_name(L"core", L"user");
}
void User::after_select()
{
}
void User::after_insert()
{
get_last_sequence_for_primary_key(L"core.user_id_seq", id);
}
void User::Clear()
{
id = -1;
name.clear();
super_user = false;
groups.clear();
email.clear();
notify = 0;
env.clear();
aenv.clear();
status = WINIX_ACCOUNT_BLOCKED;
locale_id = 0;
time_zone_id = 0;
has_pass = false;
pass_type = 0;
pass_hash_salted = false;
clear_passwords();
}
void User::clear_passwords()
{
Overwrite(password);
password.clear();
Overwrite(pass_encrypted);
pass_encrypted.clear();
}
bool User::IsMemberOf(long group) const
{
std::vector<long>::const_iterator i;
for(i=groups.cbegin() ; i!=groups.cend() ; ++i)
if( *i == group )
return true;
return false;
}
bool User::do_migration(int & current_table_version)
{
bool ok = true;
ok = ok && morm::Model::do_migration(current_table_version, 1, this, &User::do_migration_to_1);
return ok;
}
bool User::do_migration_to_1()
{
const char * str = R"sql(
CREATE TABLE core."user" (
id serial,
login character varying(255),
password character varying(255),
email character varying(255),
notify integer,
pass_type integer,
pass_hash_salted boolean,
pass_encrypted bytea,
super_user boolean,
env text,
aenv text,
status integer,
locale_id integer,
time_zone_id integer,
has_pass boolean
);
)sql";
db_query(str);
return true; // IMPROVEME remove me in the future: this is only for a moment until we do migration on all our sites
}
void User::display_name(EzcEnv & env)
{
std::wstring * dname = aenv.get_wstr(L"display_name");
if( dname && !IsWhite(*dname, true) )
env.out << *dname;
else
env.out << name;
}
} // namespace Winix