actions.py 3.34 KB
Newer Older
1
# Copyright (C) 2010-2016 GRNET S.A.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import logging
from collections import OrderedDict

from astakos.im import user_logic as users

22
from synnefo_admin.admin.actions import AdminAction
23
from synnefo_admin.admin.utils import update_actions_rbac, send_admin_email
24
from astakos.im.user_utils import change_user_email
25

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

class UserAction(AdminAction):

    """Class for actions on users. Derived from AdminAction.

    Pre-determined Attributes:
        target:        user
    """

    def __init__(self, name, f, **kwargs):
        """Initialize the class with provided values."""
        AdminAction.__init__(self, name=name, target='user', f=f, **kwargs)


def check_user_action(action):
Alex Pyrgiotis's avatar
Alex Pyrgiotis committed
41
    def check(u, action):
42 43
        res, _ = users.validate_user_action(
            u, action, verification_code=u.verification_code)
Alex Pyrgiotis's avatar
Alex Pyrgiotis committed
44 45 46
        return res

    return lambda u: check(u, action)
47 48


Alex Pyrgiotis's avatar
Alex Pyrgiotis committed
49 50 51 52
def verify(user):
    return users.verify(user, verification_code=user.verification_code)


53 54 55 56 57 58 59 60 61
def generate_actions():
    """Create a list of actions on users.

    The actions are: activate/deactivate, accept/reject, verify, contact.
    """
    actions = OrderedDict()

    actions['activate'] = UserAction(name='Activate', f=users.activate,
                                     c=check_user_action("ACTIVATE"),
Alex Pyrgiotis's avatar
Alex Pyrgiotis committed
62
                                     karma='good',)
63 64 65

    actions['deactivate'] = UserAction(name='Deactivate', f=users.deactivate,
                                       c=check_user_action("DEACTIVATE"),
Alex Pyrgiotis's avatar
Alex Pyrgiotis committed
66
                                       karma='bad', caution_level='warning',)
67 68 69

    actions['accept'] = UserAction(name='Accept', f=users.accept,
                                   c=check_user_action("ACCEPT"),
Alex Pyrgiotis's avatar
Alex Pyrgiotis committed
70
                                   karma='good',)
71 72 73

    actions['reject'] = UserAction(name='Reject', f=users.reject,
                                   c=check_user_action("REJECT"),
Alex Pyrgiotis's avatar
Alex Pyrgiotis committed
74
                                   karma='bad', caution_level='dangerous',)
75

Alex Pyrgiotis's avatar
Alex Pyrgiotis committed
76
    actions['verify'] = UserAction(name='Verify', f=verify,
77
                                   c=check_user_action("VERIFY"),
Alex Pyrgiotis's avatar
Alex Pyrgiotis committed
78
                                   karma='good',)
79

80 81 82
    actions['resend_verification'] = UserAction(
        name='Resend verification', f=users.send_verification_mail,
        karma='good', c=check_user_action("SEND_VERIFICATION_MAIL"),)
Alex Pyrgiotis's avatar
Alex Pyrgiotis committed
83

84
    actions['contact'] = UserAction(name='Send e&#8209;mail', f=send_admin_email,)
Alex Pyrgiotis's avatar
Alex Pyrgiotis committed
85

86
    actions['modify_email'] = UserAction(name='Change e&#8209;mail',
87
                                         f=change_user_email, karma='bad',
88
                                         caution_level='warning',
89
                                         data_keys=['new_email'],)
90

Alex Pyrgiotis's avatar
Alex Pyrgiotis committed
91
    update_actions_rbac(actions)
92 93

    return actions
94
cached_actions = generate_actions()