|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+# -*- coding: utf-8 -*-
|
|
|
2
|
+import transaction
|
|
|
3
|
+from sqlalchemy.exc import IntegrityError
|
|
|
4
|
+
|
|
|
5
|
+from tracim.command import AppContextCommand, Extender
|
|
|
6
|
+from tracim.lib.exception import AlreadyExistError, CommandAbortedError
|
|
|
7
|
+from tracim.lib.group import GroupApi
|
|
|
8
|
+from tracim.lib.user import UserApi
|
|
|
9
|
+from tracim.model import DBSession, User
|
|
|
10
|
+
|
|
|
11
|
+
|
|
|
12
|
+class UserCommand(AppContextCommand):
|
|
|
13
|
+
|
|
|
14
|
+ ACTION_CREATE = 'create'
|
|
|
15
|
+ ACTION_UPDATE = 'update'
|
|
|
16
|
+
|
|
|
17
|
+ action = NotImplemented
|
|
|
18
|
+
|
|
|
19
|
+ def __init__(self, *args, **kwargs):
|
|
|
20
|
+ super().__init__(*args, **kwargs)
|
|
|
21
|
+ self._session = DBSession
|
|
|
22
|
+ self._transaction = transaction
|
|
|
23
|
+ self._user_api = UserApi(None)
|
|
|
24
|
+ self._group_api = GroupApi(None)
|
|
|
25
|
+
|
|
|
26
|
+ def get_parser(self, prog_name):
|
|
|
27
|
+ parser = super().get_parser(prog_name)
|
|
|
28
|
+
|
|
|
29
|
+ parser.add_argument(
|
|
|
30
|
+ "-l",
|
|
|
31
|
+ "--login",
|
|
|
32
|
+ help='User login (email)',
|
|
|
33
|
+ dest='login',
|
|
|
34
|
+ required=True
|
|
|
35
|
+ )
|
|
|
36
|
+
|
|
|
37
|
+ parser.add_argument(
|
|
|
38
|
+ "-p",
|
|
|
39
|
+ "--password",
|
|
|
40
|
+ help='User password',
|
|
|
41
|
+ dest='password',
|
|
|
42
|
+ required=False,
|
|
|
43
|
+ default=None
|
|
|
44
|
+ )
|
|
|
45
|
+
|
|
|
46
|
+ parser.add_argument(
|
|
|
47
|
+ "-u",
|
|
|
48
|
+ "--update",
|
|
|
49
|
+ help='Update user password if exist',
|
|
|
50
|
+ dest='update',
|
|
|
51
|
+ action='store_true'
|
|
|
52
|
+ )
|
|
|
53
|
+
|
|
|
54
|
+ parser.add_argument(
|
|
|
55
|
+ "-g",
|
|
|
56
|
+ "--add-to-group",
|
|
|
57
|
+ help='Add user to group',
|
|
|
58
|
+ dest='add_to_group',
|
|
|
59
|
+ nargs='*',
|
|
|
60
|
+ action=Extender,
|
|
|
61
|
+ default=[],
|
|
|
62
|
+ )
|
|
|
63
|
+
|
|
|
64
|
+ parser.add_argument(
|
|
|
65
|
+ "-rmg",
|
|
|
66
|
+ "--remove-from-group",
|
|
|
67
|
+ help='Remove user from group',
|
|
|
68
|
+ dest='remove_from_group',
|
|
|
69
|
+ nargs='*',
|
|
|
70
|
+ action=Extender,
|
|
|
71
|
+ default=[],
|
|
|
72
|
+ )
|
|
|
73
|
+
|
|
|
74
|
+ return parser
|
|
|
75
|
+
|
|
|
76
|
+ def _user_exist(self, login):
|
|
|
77
|
+ return self._user_api.user_with_email_exists(login)
|
|
|
78
|
+
|
|
|
79
|
+ def _get_group(self, name):
|
|
|
80
|
+ return self._group_api.get_one_with_name(name)
|
|
|
81
|
+
|
|
|
82
|
+ def _add_user_to_named_group(self, user, group_name):
|
|
|
83
|
+ group = self._get_group(group_name)
|
|
|
84
|
+ if user not in group.users:
|
|
|
85
|
+ group.users.append(user)
|
|
|
86
|
+ self._session.flush()
|
|
|
87
|
+
|
|
|
88
|
+ def _remove_user_from_named_group(self, user, group_name):
|
|
|
89
|
+ group = self._get_group(group_name)
|
|
|
90
|
+ if user in group.users:
|
|
|
91
|
+ group.users.remove(user)
|
|
|
92
|
+ self._session.flush()
|
|
|
93
|
+
|
|
|
94
|
+ def _create_user(self, login, password, **kwargs):
|
|
|
95
|
+ if not password:
|
|
|
96
|
+ raise CommandAbortedError("You must provide -p/--password parameter")
|
|
|
97
|
+
|
|
|
98
|
+ try:
|
|
|
99
|
+ user = User(email=login, password=password, **kwargs)
|
|
|
100
|
+ self._session.add(user)
|
|
|
101
|
+ self._session.flush()
|
|
|
102
|
+ except IntegrityError:
|
|
|
103
|
+ self._session.rollback()
|
|
|
104
|
+ raise AlreadyExistError()
|
|
|
105
|
+
|
|
|
106
|
+ return user
|
|
|
107
|
+
|
|
|
108
|
+ def _update_password_for_login(self, login, password):
|
|
|
109
|
+ user = self._user_api.get_one_by_email(login)
|
|
|
110
|
+ user.password = password
|
|
|
111
|
+ self._session.flush()
|
|
|
112
|
+ transaction.commit()
|
|
|
113
|
+
|
|
|
114
|
+ def take_action(self, parsed_args):
|
|
|
115
|
+ super().take_action(parsed_args)
|
|
|
116
|
+
|
|
|
117
|
+ user = self._proceed_user(parsed_args)
|
|
|
118
|
+ self._proceed_groups(user, parsed_args)
|
|
|
119
|
+
|
|
|
120
|
+ print("User created/updated")
|
|
|
121
|
+
|
|
|
122
|
+ def _proceed_user(self, parsed_args):
|
|
|
123
|
+ if self.action == self.ACTION_CREATE:
|
|
|
124
|
+ try:
|
|
|
125
|
+ user = self._create_user(login=parsed_args.login, password=parsed_args.password)
|
|
|
126
|
+ except AlreadyExistError:
|
|
|
127
|
+ raise CommandAbortedError("Error: User already exist (use `user update` command instead)")
|
|
|
128
|
+ else:
|
|
|
129
|
+ if parsed_args.password:
|
|
|
130
|
+ self._update_password_for_login(login=parsed_args.login, password=parsed_args.password)
|
|
|
131
|
+ user = self._user_api.get_one_by_email(parsed_args.login)
|
|
|
132
|
+
|
|
|
133
|
+ return user
|
|
|
134
|
+
|
|
|
135
|
+ def _proceed_groups(self, user, parsed_args):
|
|
|
136
|
+ # User always in "users" group
|
|
|
137
|
+ self._add_user_to_named_group(user, 'users')
|
|
|
138
|
+
|
|
|
139
|
+ for group_name in parsed_args.add_to_group:
|
|
|
140
|
+ self._add_user_to_named_group(user, group_name)
|
|
|
141
|
+
|
|
|
142
|
+ for group_name in parsed_args.remove_from_group:
|
|
|
143
|
+ self._remove_user_from_named_group(user, group_name)
|
|
|
144
|
+
|
|
|
145
|
+
|
|
|
146
|
+class CreateUserCommand(UserCommand):
|
|
|
147
|
+ action = UserCommand.ACTION_CREATE
|
|
|
148
|
+
|
|
|
149
|
+
|
|
|
150
|
+class UpdateUserCommand(UserCommand):
|
|
|
151
|
+ action = UserCommand.ACTION_UPDATE
|