Completed
Pull Request — master (#2920)
by Anthony
07:24 queued 01:55
created

User.get_by_chatops_id()   A

Complexity

Conditions 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
c 1
b 0
f 1
dl 0
loc 8
rs 9.4285
1
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
2
# contributor license agreements.  See the NOTICE file distributed with
3
# this work for additional information regarding copyright ownership.
4
# The ASF licenses this file to You under the Apache License, Version 2.0
5
# (the "License"); you may not use this file except in compliance with
6
# the License.  You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
16
from st2common.exceptions.auth import (TokenNotFoundError, ApiKeyNotFoundError,
17
                                       UserNotFoundError)
18
from st2common.models.db import MongoDBAccess
19
from st2common.models.db.auth import UserDB, TokenDB, ApiKeyDB
20
from st2common.persistence.base import Access
21
from st2common.util import hash as hash_utils
22
23
24
class User(Access):
25
    impl = MongoDBAccess(UserDB)
26
27
    @classmethod
28
    def get(cls, username):
0 ignored issues
show
Bug introduced by
Arguments number differs from overridden 'get' method
Loading history...
29
        return cls.get_by_name(username)
30
31
    @classmethod
32
    def get_by_chatops_id(cls, value):
33
        result = cls.query(chatops_id=value).first()
34
35
        if not result:
36
            raise UserNotFoundError()
37
38
        return result
39
40
    @classmethod
41
    def _get_impl(cls):
42
        return cls.impl
43
44
    @classmethod
45
    def _get_by_object(cls, object):
0 ignored issues
show
Bug Best Practice introduced by
This seems to re-define the built-in object.

It is generally discouraged to redefine built-ins as this makes code very hard to read.

Loading history...
46
        # For User name is unique.
47
        name = getattr(object, 'name', '')
48
        return cls.get_by_name(name)
49
50
51
class Token(Access):
52
    impl = MongoDBAccess(TokenDB)
53
54
    @classmethod
55
    def _get_impl(cls):
56
        return cls.impl
57
58
    @classmethod
59
    def add_or_update(cls, model_object, publish=True):
0 ignored issues
show
Bug introduced by
Arguments number differs from overridden 'add_or_update' method
Loading history...
60
        if not getattr(model_object, 'user', None):
61
            raise ValueError('User is not provided in the token.')
62
        if not getattr(model_object, 'token', None):
63
            raise ValueError('Token value is not set.')
64
        if not getattr(model_object, 'expiry', None):
65
            raise ValueError('Token expiry is not provided in the token.')
66
        return super(Token, cls).add_or_update(model_object, publish=publish)
67
68
    @classmethod
69
    def get(cls, value):
0 ignored issues
show
Bug introduced by
Arguments number differs from overridden 'get' method
Loading history...
70
        result = cls.query(token=value).first()
71
72
        if not result:
73
            raise TokenNotFoundError()
74
75
        return result
76
77
78
class ApiKey(Access):
79
    impl = MongoDBAccess(ApiKeyDB)
80
81
    @classmethod
82
    def _get_impl(cls):
83
        return cls.impl
84
85
    @classmethod
86
    def get(cls, value):
0 ignored issues
show
Bug introduced by
Arguments number differs from overridden 'get' method
Loading history...
87
        # DB does not contain key but the key_hash.
88
        value_hash = hash_utils.hash(value)
89
        result = cls.query(key_hash=value_hash).first()
90
91
        if not result:
92
            raise ApiKeyNotFoundError('ApiKey with key_hash=%s not found.' % value_hash)
93
94
        return result
95
96
    @classmethod
97
    def get_by_key_or_id(cls, value):
98
        try:
99
            return cls.get(value)
100
        except ApiKeyNotFoundError:
101
            pass
102
        try:
103
            return cls.get_by_id(value)
104
        except:
105
            raise ApiKeyNotFoundError('ApiKey with key or id=%s not found.' % value)
106