summaryrefslogtreecommitdiff
path: root/gentoobrowse-api/service/usersimpl.cpp
blob: 83aba65f941f3ba9edec201002a9d973d6f402a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "usersimpl.h"
#include "users.h"
#include <Ice/Communicator.h>
#include <Ice/Current.h>
#include <Ice/ObjectAdapter.h>
#include <Ice/Optional.h>
#include <Ice/Properties.h>
#include <abstractCachingDatabaseClient.h>
#include <abstractDatabaseClient.h>
#include <connectionPool.h>
#include <icecube.h>
#include <mail.h>
#include <memory>
#include <notifications.h>
#include <regex>
#include <sql/users/authenticate.sql.h>
#include <sql/users/create.sql.h>
#include <sql/users/find.sql.h>
#include <sql/users/get.sql.h>
#include <sql/users/getNew.sql.h>
#include <sql/users/mailshotsent.sql.h>
#include <sql/users/prune.sql.h>
#include <sql/users/safeDelete.sql.h>
#include <sql/users/track.sql.h>
#include <sql/users/tracked.sql.h>
#include <sql/users/untrack.sql.h>
#include <sql/users/verify.sql.h>
#include <staticSqlSource.h>
#include <vector>

// IWYU pragma: no_include "resourcePool.impl.h"

namespace Gentoo::Service {
	Users::Users(const DB::ConnectionPoolPtr & ro, const DB::ConnectionPoolPtr & rw) : rodb {ro}, rwdb {rw} { }

	Gentoo::UserPtr
	Users::authenticate(const std::string_view username, const std::string_view password, const Ice::Current &)
	{
		return rodb.fetchCache<Gentoo::UserPtr>(sql::users::authenticate, 30, username, password);
	}

	Gentoo::UserPtr
	Users::verify(const std::string_view username, const std::string_view verifyguid, const Ice::Current &)
	{
		return rwdb.fetch<Gentoo::UserPtr>(sql::users::verify, username, verifyguid);
	}

	Gentoo::UserPtr
	Users::get(Ice::Int id, const Ice::Current &)
	{
		return rodb.fetch<Gentoo::UserPtr>(sql::users::get, id);
	}

	Gentoo::NewUserPtr
	Users::getNew(const std::string_view username, const std::string_view password, const Ice::Current &)
	{
		return rodb.fetch<Gentoo::NewUserPtr>(sql::users::getNew, username, password);
	}

	Gentoo::UserPtr
	Users::find(const std::string_view username, const Ice::Current &)
	{
		return rodb.fetch<Gentoo::UserPtr>(sql::users::find, username);
	}

	template<typename C>
	Gentoo::NewUserPtr
	Users::authOrCreate(IceTray::TransactionalDatabaseClient<C> & db, const std::string_view & username,
			const std::string_view & password, const std::string_view & realname, const std::string_view & email)
	{
		auto existing
				= db.template fetch<IceUtil::Optional<Gentoo::NewUserPtr>>(sql::users::getNew, username, password);
		if (existing && *existing) {
			return *existing;
		}
		return db.template fetch<Gentoo::NewUserPtr>(sql::users::create, username, password, realname, email);
	}

	Gentoo::NewUserPtr
	Users::create(const std::string_view username, const std::string_view password, const std::string_view realname,
			const std::string_view email, const Ice::Current &)
	{
		static const std::regex invalid {".*://.*"};
		static const std::regex validUsername {".+"};
		static const std::regex validRealname {".+"};
		static const std::regex validEmail {"[^ ]+@[^ ]+(\\.[^ ]+)+"};

		auto notifications = IceTray::Cube::get<Notifications>();
		auto mailServer = IceTray::Cube::get<IceTray::Mail::MailServer>();

		const auto check = [](const auto in, const auto & valid, const auto & invalid) {
			if (!std::regex_match(in.begin(), in.end(), valid) || std::regex_match(in.begin(), in.end(), invalid)) {
				throw Rejected {};
			}
		};
		check(username, validUsername, invalid);
		check(realname, validRealname, invalid);
		check(email, validEmail, invalid);

		auto tx = rwdb.transactional();
		auto newUser = authOrCreate(tx, username, password, realname, email);
		auto mail = notifications->getSignup(newUser);
		mailServer->sendEmail(mail);

		return newUser;
	}

	void
	Users::mailshotsent(Ice::Int id, const Ice::Current &)
	{
		rwdb.modify(sql::users::mailshotsent, id);
	}

	void
	Users::remove(Ice::Int id, const std::string_view password, const Ice::Current &)
	{
		rwdb.modify(sql::users::safeDelete, id, password);
	}

	void
	Users::prune(const Ice::Current & current)
	{
		auto properties = current.adapter->getCommunicator()->getProperties();
		auto prunePeriod = properties->getPropertyWithDefault("GentooBrowseAPI.Users.PrunePeriod", "8 weeks");
		rwdb.modify(sql::users::prune, prunePeriod);
	}

	void
	Users::track(Ice::Int userId, Ice::Int packageId, const Ice::Current &)
	{
		rwdb.modify(sql::users::track, userId, packageId);
	}

	void
	Users::untrack(Ice::Int userId, Ice::Int packageId, const Ice::Current &)
	{
		rwdb.modify(sql::users::untrack, userId, packageId);
	}

	Gentoo::PackageIds
	Users::tracked(Ice::Int userId, const Ice::Current &)
	{
		return rodb.fetchCache<Gentoo::PackageIds>(sql::users::tracked, 10, userId);
	}
}