2012-05-30 21:04:18 +02:00
|
|
|
/*
|
|
|
|
* This file is a part of Winix
|
2014-10-04 20:04:03 +02:00
|
|
|
* and is distributed under the 2-Clause BSD licence.
|
|
|
|
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2022-11-14 03:22:48 +01:00
|
|
|
* Copyright (c) 2012-2022, Tomasz Sowa
|
2012-05-30 21:04:18 +02:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
2014-10-04 20:04:03 +02:00
|
|
|
* 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.
|
|
|
|
*
|
2012-05-30 21:04:18 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "user.h"
|
2021-04-30 01:34:48 +02:00
|
|
|
#include "core/misc.h"
|
2021-06-19 23:48:12 +02:00
|
|
|
#include "templates/templates.h"
|
2012-05-30 21:04:18 +02:00
|
|
|
|
|
|
|
|
2014-02-12 17:30:49 +01:00
|
|
|
namespace Winix
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-05-30 21:04:18 +02:00
|
|
|
User::User()
|
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-04-30 01:34:48 +02:00
|
|
|
|
|
|
|
|
2021-05-13 03:29:41 +02:00
|
|
|
void User::fields()
|
2021-04-30 01:34:48 +02:00
|
|
|
{
|
2021-06-19 23:48:12 +02:00
|
|
|
field(L"id", id, morm::FT::no_insertable | morm::FT::no_updatable | morm::FT::primary_key);
|
2021-06-27 23:31:50 +02:00
|
|
|
field(L"login", login);
|
|
|
|
field(L"is_super_user", is_super_user);
|
2021-06-19 23:48:12 +02:00
|
|
|
|
|
|
|
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);
|
2021-06-27 23:31:50 +02:00
|
|
|
field(L"is_pass_hash_salted", is_pass_hash_salted);
|
2021-06-19 23:48:12 +02:00
|
|
|
|
|
|
|
field(L"email", email);
|
|
|
|
field(L"notify", notify);
|
|
|
|
field(L"env", env);
|
2021-06-27 23:31:50 +02:00
|
|
|
field(L"admin_env", admin_env);
|
2021-06-19 23:48:12 +02:00
|
|
|
field(L"status", status);
|
|
|
|
field(L"locale_id", locale_id);
|
|
|
|
field(L"time_zone_id", time_zone_id);
|
2021-06-27 23:31:50 +02:00
|
|
|
|
|
|
|
field(L"", L"id_is", &User::id_is);
|
|
|
|
field(L"", L"is_guest", &User::is_guest);
|
|
|
|
field(L"", L"is_env_object", &User::is_env_object);
|
|
|
|
field(L"", L"is_admin_env_object", &User::is_admin_env_object);
|
|
|
|
|
2021-04-30 01:34:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-13 03:29:41 +02:00
|
|
|
void User::table()
|
2021-04-30 01:34:48 +02:00
|
|
|
{
|
2021-05-13 03:29:41 +02:00
|
|
|
table_name(L"core", L"user");
|
2021-04-30 01:34:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void User::after_select()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void User::after_insert()
|
|
|
|
{
|
|
|
|
get_last_sequence_for_primary_key(L"core.user_id_seq", id);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-05-30 21:04:18 +02:00
|
|
|
void User::Clear()
|
|
|
|
{
|
|
|
|
id = -1;
|
2021-06-27 23:31:50 +02:00
|
|
|
login.clear();
|
|
|
|
is_super_user = false;
|
2012-05-30 21:04:18 +02:00
|
|
|
groups.clear();
|
|
|
|
email.clear();
|
|
|
|
notify = 0;
|
2021-04-09 17:50:58 +02:00
|
|
|
env.clear();
|
2021-06-27 23:31:50 +02:00
|
|
|
admin_env.clear();
|
2012-05-30 21:04:18 +02:00
|
|
|
status = WINIX_ACCOUNT_BLOCKED;
|
2012-06-27 01:19:19 +02:00
|
|
|
locale_id = 0;
|
|
|
|
time_zone_id = 0;
|
2021-04-30 01:34:48 +02:00
|
|
|
|
|
|
|
has_pass = false;
|
|
|
|
pass_type = 0;
|
2021-06-27 23:31:50 +02:00
|
|
|
is_pass_hash_salted = false;
|
2021-04-30 01:34:48 +02:00
|
|
|
clear_passwords();
|
2012-05-30 21:04:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-04-30 01:34:48 +02:00
|
|
|
void User::clear_passwords()
|
|
|
|
{
|
|
|
|
Overwrite(password);
|
|
|
|
password.clear();
|
|
|
|
|
|
|
|
Overwrite(pass_encrypted);
|
|
|
|
pass_encrypted.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-06-07 13:20:44 +02:00
|
|
|
|
2021-06-18 19:18:13 +02:00
|
|
|
bool User::IsMemberOf(long group) const
|
2012-05-30 21:04:18 +02:00
|
|
|
{
|
2021-06-18 19:18:13 +02:00
|
|
|
std::vector<long>::const_iterator i;
|
2012-05-30 21:04:18 +02:00
|
|
|
|
2021-06-18 19:18:13 +02:00
|
|
|
for(i=groups.cbegin() ; i!=groups.cend() ; ++i)
|
2012-05-30 21:04:18 +02:00
|
|
|
if( *i == group )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-14 03:31:29 +02:00
|
|
|
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);
|
2021-06-27 23:31:50 +02:00
|
|
|
ok = ok && morm::Model::do_migration(current_table_version, 2, this, &User::do_migration_to_2);
|
2022-12-19 19:13:45 +01:00
|
|
|
ok = ok && morm::Model::do_migration(current_table_version, 3, this, &User::do_migration_to_3);
|
2021-05-14 03:31:29 +02:00
|
|
|
|
|
|
|
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,
|
2022-04-11 23:19:30 +02:00
|
|
|
pass_hash_salted boolean,
|
2021-05-14 03:31:29 +02:00
|
|
|
pass_encrypted bytea,
|
2022-04-11 23:19:30 +02:00
|
|
|
super_user boolean,
|
2021-05-14 03:31:29 +02:00
|
|
|
env text,
|
2022-04-11 20:44:58 +02:00
|
|
|
aenv text,
|
2021-05-14 03:31:29 +02:00
|
|
|
status integer,
|
|
|
|
locale_id integer,
|
|
|
|
time_zone_id integer,
|
|
|
|
has_pass boolean
|
|
|
|
);
|
|
|
|
)sql";
|
|
|
|
|
2022-04-11 23:19:30 +02:00
|
|
|
return db_query(str);
|
2021-05-14 03:31:29 +02:00
|
|
|
return true; // IMPROVEME remove me in the future: this is only for a moment until we do migration on all our sites
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-27 23:31:50 +02:00
|
|
|
bool User::do_migration_to_2()
|
|
|
|
{
|
|
|
|
const char * str[] = {
|
|
|
|
"alter table core.user rename column aenv to admin_env",
|
|
|
|
"alter table core.user rename column super_user to is_super_user",
|
|
|
|
"alter table core.user rename column pass_hash_salted to is_pass_hash_salted",
|
|
|
|
};
|
|
|
|
|
|
|
|
size_t len = sizeof(str) / sizeof(const char*);
|
|
|
|
return db_query(str, len);
|
|
|
|
}
|
|
|
|
|
2021-05-14 03:31:29 +02:00
|
|
|
|
2022-12-19 19:13:45 +01:00
|
|
|
bool User::do_migration_to_3()
|
|
|
|
{
|
|
|
|
const char * str[] = {
|
|
|
|
"ALTER TABLE core.\"user\" ADD CONSTRAINT user_pkey PRIMARY KEY (id);",
|
|
|
|
};
|
|
|
|
|
|
|
|
size_t len = sizeof(str) / sizeof(const char*);
|
|
|
|
return db_query(str, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-19 23:48:12 +02:00
|
|
|
void User::display_name(EzcEnv & env)
|
|
|
|
{
|
2021-06-27 23:31:50 +02:00
|
|
|
std::wstring * dname = admin_env.get_wstr(L"display_name");
|
2021-06-19 23:48:12 +02:00
|
|
|
|
|
|
|
if( dname && !IsWhite(*dname, true) )
|
|
|
|
env.out << *dname;
|
|
|
|
else
|
2021-06-27 23:31:50 +02:00
|
|
|
env.out << login;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void User::id_is(EzcEnv & env)
|
|
|
|
{
|
|
|
|
if( !env.par.empty() )
|
|
|
|
{
|
2022-11-14 03:22:48 +01:00
|
|
|
long par_user_id = pt::to_l(env.par);
|
2021-06-27 23:31:50 +02:00
|
|
|
env.res = id == par_user_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool User::is_guest()
|
|
|
|
{
|
|
|
|
return id == -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool User::is_env_object()
|
|
|
|
{
|
|
|
|
return env.is_object();
|
2021-06-19 23:48:12 +02:00
|
|
|
}
|
2021-05-14 03:31:29 +02:00
|
|
|
|
2012-05-30 21:04:18 +02:00
|
|
|
|
2021-06-27 23:31:50 +02:00
|
|
|
bool User::is_admin_env_object()
|
|
|
|
{
|
|
|
|
return admin_env.is_object();
|
|
|
|
}
|
|
|
|
|
2012-05-30 21:04:18 +02:00
|
|
|
|
|
|
|
|
2014-02-12 17:30:49 +01:00
|
|
|
} // namespace Winix
|
2012-05-30 21:04:18 +02:00
|
|
|
|
|
|
|
|