trac#687: Add unit tests for `redirect` and `redirect_obj`.
[larjonas-mediagoblin.git] / mediagoblin / gmg_commands / users.py
blobd1a8b72df7f9b5f9ae8107891f0c84ffe07df40e
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 from __future__ import print_function
19 import sys
21 import six
23 from mediagoblin.db.models import LocalUser
24 from mediagoblin.gmg_commands import util as commands_util
25 from mediagoblin import auth
26 from mediagoblin import mg_globals
28 def adduser_parser_setup(subparser):
29 subparser.add_argument(
30 '--username','-u',
31 help="Username used to login")
32 subparser.add_argument(
33 '--password','-p',
34 help="Your supersecret word to login, beware of storing it in bash history")
35 subparser.add_argument(
36 '--email','-e',
37 help="Email to receive notifications")
40 def adduser(args):
41 #TODO: Lets trust admins this do not validate Emails :)
42 commands_util.setup_app(args)
44 args.username = six.text_type(commands_util.prompt_if_not_set(args.username, "Username:"))
45 args.password = commands_util.prompt_if_not_set(args.password, "Password:",True)
46 args.email = commands_util.prompt_if_not_set(args.email, "Email:")
48 db = mg_globals.database
49 users_with_username = \
50 db.LocalUser.query.filter(
51 LocalUser.username==args.username.lower()
52 ).count()
54 if users_with_username:
55 print(u'Sorry, a user with that name already exists.')
56 sys.exit(1)
58 else:
59 # Create the user
60 entry = db.LocalUser()
61 entry.username = six.text_type(args.username.lower())
62 entry.email = six.text_type(args.email)
63 entry.pw_hash = auth.gen_password_hash(args.password)
64 default_privileges = [
65 db.Privilege.query.filter(
66 db.Privilege.privilege_name==u'commenter').one(),
67 db.Privilege.query.filter(
68 db.Privilege.privilege_name==u'uploader').one(),
69 db.Privilege.query.filter(
70 db.Privilege.privilege_name==u'reporter').one(),
71 db.Privilege.query.filter(
72 db.Privilege.privilege_name==u'active').one()
74 entry.all_privileges = default_privileges
75 entry.save()
77 print(u"User created (and email marked as verified).")
80 def makeadmin_parser_setup(subparser):
81 subparser.add_argument(
82 'username',
83 help="Username to give admin level",
84 type=six.text_type)
87 def makeadmin(args):
88 commands_util.setup_app(args)
90 db = mg_globals.database
92 user = db.LocalUser.query.filter(
93 LocalUser.username==args.username.lower()
94 ).first()
95 if user:
96 user.all_privileges.append(
97 db.Privilege.query.filter(
98 db.Privilege.privilege_name==u'admin').one()
100 user.save()
101 print(u'The user %s is now an admin.' % args.username)
102 else:
103 print(u'The user %s doesn\'t exist.' % args.username)
104 sys.exit(1)
107 def changepw_parser_setup(subparser):
108 subparser.add_argument(
109 'username',
110 help="Username used to login",
111 type=six.text_type)
112 subparser.add_argument(
113 'password',
114 help="Your NEW supersecret word to login")
117 def changepw(args):
118 commands_util.setup_app(args)
120 db = mg_globals.database
122 user = db.LocalUser.query.filter(
123 LocalUser.username==args.username.lower()
124 ).first()
125 if user:
126 user.pw_hash = auth.gen_password_hash(args.password)
127 user.save()
128 print(u'Password successfully changed for user %s.' % args.username)
129 else:
130 print(u'The user %s doesn\'t exist.' % args.username)
131 sys.exit(1)
134 def deleteuser_parser_setup(subparser):
135 subparser.add_argument(
136 'username',
137 help="Username to delete",
138 type=six.text_type)
141 def deleteuser(args):
142 commands_util.setup_app(args)
144 db = mg_globals.database
146 user = db.LocalUser.query.filter(
147 LocalUser.username==args.username.lower()
148 ).first()
149 if user:
150 user.delete()
151 print('The user %s has been deleted.' % args.username)
152 else:
153 print('The user %s doesn\'t exist.' % args.username)
154 sys.exit(1)