This commit was the work I did fixing errors that cropped up from the merge.
[mediagoblin.git] / mediagoblin / gmg_commands / users.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 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 import auth
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.query.filter_by(
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.gen_password_hash(args.password)
56 entry.status = u'active'
57 entry.email_verified = True
58 default_privileges = [
59 db.Privilege.query.filter(
60 db.Privilege.privilege_name==u'commenter').one(),
61 db.Privilege.query.filter(
62 db.Privilege.privilege_name==u'uploader').one(),
63 db.Privilege.query.filter(
64 db.Privilege.privilege_name==u'reporter').one(),
65 db.Privilege.query.filter(
66 db.Privilege.privilege_name==u'active').one()
67 ]
68 entry.all_privileges = default_privileges
69 entry.save()
70
71 print "User created (and email marked as verified)"
72
73
74 def makeadmin_parser_setup(subparser):
75 subparser.add_argument(
76 'username',
77 help="Username to give admin level")
78
79
80 def makeadmin(args):
81 commands_util.setup_app(args)
82
83 db = mg_globals.database
84
85 user = db.User.query.filter_by(
86 username=unicode(args.username.lower())).one()
87 if user:
88 user.is_admin = True
89 user.all_privileges.append(
90 db.Privilege.query.filter(
91 db.Privilege.privilege_name==u'admin').one()
92 )
93 user.save()
94 print 'The user is now Admin'
95 else:
96 print 'The user doesn\'t exist'
97
98
99 def changepw_parser_setup(subparser):
100 subparser.add_argument(
101 'username',
102 help="Username used to login")
103 subparser.add_argument(
104 'password',
105 help="Your NEW supersecret word to login")
106
107
108 def changepw(args):
109 commands_util.setup_app(args)
110
111 db = mg_globals.database
112
113 user = db.User.query.filter_by(
114 username=unicode(args.username.lower())).one()
115 if user:
116 user.pw_hash = auth.gen_password_hash(args.password)
117 user.save()
118 print 'Password successfully changed'
119 else:
120 print 'The user doesn\'t exist'