Merge remote-tracking branch 'refs/remotes/upstream/master' into 569-application...
[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',
25 help="Username used to login")
26 subparser.add_argument(
27 'password',
28 help="Your supersecret word to login")
29 subparser.add_argument(
30 'email',
31 help="Email to recieve notifications")
32 subparser.add_argument(
33 '-cf', '--conf_file', default='mediagoblin.ini',
34 help="Config file used to set up environment")
35
36
37 def adduser(args):
38 #TODO: Lets trust admins this do not validate Emails :)
39 commands_util.setup_app(args)
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 subparser.add_argument(
68 '-cf', '--conf_file', default='mediagoblin.ini',
69 help="Config file used to set up environment")
70
71
72 def makeadmin(args):
73 commands_util.setup_app(args)
74
75 db = mg_globals.database
76
77 user = db.User.one({'username':unicode(args.username.lower())})
78 if user:
79 user['is_admin'] = True
80 user.save()
81 print 'The user is now Admin'
82 else:
83 print 'The user doesn\'t exist'
84
85
86 def changepw_parser_setup(subparser):
87 subparser.add_argument(
88 'username',
89 help="Username used to login")
90 subparser.add_argument(
91 'password',
92 help="Your NEW supersecret word to login")
93 subparser.add_argument(
94 '-cf', '--conf_file', default='mediagoblin.ini',
95 help="Config file used to set up environment")
96
97
98 def changepw(args):
99 commands_util.setup_app(args)
100
101 db = mg_globals.database
102
103 user = db.User.one({'username':unicode(args.username.lower())})
104 if user:
105 user['pw_hash'] = auth_lib.bcrypt_gen_password_hash(args.password)
106 user.save()
107 print 'Password successfully changed'
108 else:
109 print 'The user doesn\'t exist'
110