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