aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Interpreters/Access/InterpreterMoveAccessEntityQuery.cpp
blob: 49e90783a59fdfc5823e94f620f2d507a3b44f20 (plain) (blame)
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
#include <Interpreters/Access/InterpreterMoveAccessEntityQuery.h>
#include <Parsers/Access/ASTMoveAccessEntityQuery.h>
#include <Parsers/Access/ASTRowPolicyName.h>
#include <Access/AccessControl.h>
#include <Access/Common/AccessRightsElement.h>
#include <Interpreters/executeDDLQueryOnCluster.h>


namespace DB
{
namespace ErrorCodes
{
    extern const int NOT_IMPLEMENTED;
    extern const int ACCESS_ENTITY_NOT_FOUND;
}


BlockIO InterpreterMoveAccessEntityQuery::execute()
{
    auto & query = query_ptr->as<ASTMoveAccessEntityQuery &>();
    auto & access_control = getContext()->getAccessControl();
    getContext()->checkAccess(getRequiredAccess());

    if (!query.cluster.empty())
        return executeDDLQueryOnCluster(query_ptr, getContext());

    query.replaceEmptyDatabase(getContext()->getCurrentDatabase());

    std::vector<UUID> ids;
    if (query.type == AccessEntityType::ROW_POLICY)
        ids = access_control.getIDs(query.type, query.row_policy_names->toStrings());
    else
        ids = access_control.getIDs(query.type, query.names);

    /// Validate that all entities are from the same storage.
    const auto source_storage = access_control.findStorage(ids.front());
    if (!source_storage->exists(ids))
        throw Exception(ErrorCodes::ACCESS_ENTITY_NOT_FOUND, "All access entities must be from the same storage in order to be moved");

    access_control.moveAccessEntities(ids, source_storage->getStorageName(), query.storage_name);
    return {};
}


AccessRightsElements InterpreterMoveAccessEntityQuery::getRequiredAccess() const
{
    const auto & query = query_ptr->as<const ASTMoveAccessEntityQuery &>();
    AccessRightsElements res;
    switch (query.type)
    {
        case AccessEntityType::USER:
        {
            res.emplace_back(AccessType::DROP_USER);
            res.emplace_back(AccessType::CREATE_USER);
            return res;
        }
        case AccessEntityType::ROLE:
        {
            res.emplace_back(AccessType::DROP_ROLE);
            res.emplace_back(AccessType::CREATE_ROLE);
            return res;
        }
        case AccessEntityType::SETTINGS_PROFILE:
        {
            res.emplace_back(AccessType::DROP_SETTINGS_PROFILE);
            res.emplace_back(AccessType::CREATE_SETTINGS_PROFILE);
            return res;
        }
        case AccessEntityType::ROW_POLICY:
        {
            if (query.row_policy_names)
            {
                for (const auto & row_policy_name : query.row_policy_names->full_names)
                {
                    res.emplace_back(AccessType::DROP_ROW_POLICY, row_policy_name.database, row_policy_name.table_name);
                    res.emplace_back(AccessType::CREATE_ROW_POLICY, row_policy_name.database, row_policy_name.table_name);
                }
            }
            return res;
        }
        case AccessEntityType::QUOTA:
        {
            res.emplace_back(AccessType::DROP_QUOTA);
            res.emplace_back(AccessType::CREATE_QUOTA);
            return res;
        }
        case AccessEntityType::MAX:
            break;
    }
    throw Exception(ErrorCodes::NOT_IMPLEMENTED, "{}: type is not supported by DROP query", toString(query.type));
}

}