Change adduser arguments from positional to --keyword style.
[mediagoblin.git] / mediagoblin / gmg_commands / users.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 MediaGoblin contributors. See AUTHORS.
3 #
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.
8 #
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.
13 #
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/>.
16
17 from mediagoblin.gmg_commands import util as commands_util
18 from mediagoblin.auth import lib as auth_lib
19 from mediagoblin import mg_globals
20
21
22 def adduser_parser_setup(subparser):
23 subparser.add_argument(
24 '--username','-u',
25 help="Username used to login")
26 subparser.add_argument(
27 '--password','-p',
28 help="Your supersecret word to login, beware of storing it in bash history")
29 subparser.add_argument(
30 '--email','-e',
31 help="Email to receive notifications")
32
33
34 def adduser(args):
35 #TODO: Lets trust admins this do not validate Emails :)
36 commands_util.setup_app(args)
37
38 db = mg_globals.database
39 users_with_username = \
40 db.User.find({
41 'username': args.username.lower(),
42 }).count()
43
44 if users_with_username:
45 print u'Sorry, a user with that name already exists.'
46
47 else:
48 # Create the user
49 entry = db.User()
50 entry['username'] = unicode(args.username.lower())
51 entry['email'] = unicode(args.email)
52 entry['pw_hash'] = auth_lib.bcrypt_gen_password_hash(args.password)
53 entry['status'] = u'active'
54 entry['email_verified'] = True
55 entry.save(validate=True)
56
57 print "User created (and email marked as verified)"
58
59
60 def makeadmin_parser_setup(subparser):
61 subparser.add_argument(
62 'username',
63 help="Username to give admin level")
64
65
66 def makeadmin(args):
67 commands_util.setup_app(args)
68
69 db = mg_globals.database
70
71 user = db.User.one({'username': unicode(args.username.lower())})
72 if user:
73 user['is_admin'] = True
74 user.save()
75 print 'The user is now Admin'
76 else:
77 print 'The user doesn\'t exist'
78
79
80 def changepw_parser_setup(subparser):
81 subparser.add_argument(
82 'username',
83 help="Username used to login")
84 subparser.add_argument(
85 'password',
86 help="Your NEW supersecret word to login")
87
88
89 def changepw(args):
90 commands_util.setup_app(args)
91
92 db = mg_globals.database
93
94 user = db.User.one({'username': unicode(args.username.lower())})
95 if user:
96 user['pw_hash'] = auth_lib.bcrypt_gen_password_hash(args.password)
97 user.save()
98 print 'Password successfully changed'
99 else:
100 print 'The user doesn\'t exist'