userver: userver/server/auth/user_scopes.hpp Source File
Loading...
Searching...
No Matches
user_scopes.hpp
1#pragma once
2
3#include <functional>
4#include <string>
5#include <string_view>
6#include <vector>
7
8#include <userver/formats/parse/to.hpp>
9
10USERVER_NAMESPACE_BEGIN
11
12namespace server::auth {
13
14class UserScope final {
15public:
16 explicit UserScope(std::string value)
17 : value_(std::move(value))
18 {}
19
20 const std::string& GetValue() const { return value_; }
21
22private:
23 std::string value_;
24};
25
26inline bool operator==(const UserScope& lhs, const UserScope& rhs) { return lhs.GetValue() == rhs.GetValue(); }
27
28inline bool operator!=(const UserScope& lhs, const UserScope& rhs) { return lhs.GetValue() != rhs.GetValue(); }
29
30inline bool operator==(const UserScope& lhs, std::string_view rhs) { return lhs.GetValue() == rhs; }
31
32inline bool operator==(std::string_view lhs, const UserScope& rhs) { return lhs == rhs.GetValue(); }
33
34inline bool operator!=(const UserScope& lhs, std::string_view rhs) { return lhs.GetValue() != rhs; }
35
36inline bool operator!=(std::string_view lhs, const UserScope& rhs) { return lhs != rhs.GetValue(); }
37
38template <class Value>
39UserScope Parse(const Value& v, formats::parse::To<UserScope>) {
40 return UserScope{v.template As<std::string>()};
41}
42
43using UserScopes = std::vector<UserScope>;
44
45} // namespace server::auth
46
47USERVER_NAMESPACE_END
48
49namespace std {
50template <>
51struct hash<USERVER_NAMESPACE::server::auth::UserScope> {
52 std::size_t operator()(const USERVER_NAMESPACE::server::auth::UserScope& v) const {
53 return std::hash<std::string>{}(v.GetValue());
54 }
55};
56} // namespace std