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