Merge branch 'master' into merge-python3-port
[mediagoblin.git] / mediagoblin / gmg_commands / users.py
CommitLineData
fc3dc255 1# GNU MediaGoblin -- federated, autonomous media hosting
cf29e8a8 2# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
fc3dc255
WKG
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
f9d93c0e
BP
17from __future__ import print_function
18
e49b7e02
BP
19import six
20
96108ea2 21from mediagoblin.gmg_commands import util as commands_util
9c2c9be7 22from mediagoblin import auth
96108ea2
AV
23from mediagoblin import mg_globals
24
96108ea2
AV
25def adduser_parser_setup(subparser):
26 subparser.add_argument(
d4630553 27 '--username','-u',
96108ea2
AV
28 help="Username used to login")
29 subparser.add_argument(
d4630553
MUS
30 '--password','-p',
31 help="Your supersecret word to login, beware of storing it in bash history")
96108ea2 32 subparser.add_argument(
d4630553
MUS
33 '--email','-e',
34 help="Email to receive notifications")
96108ea2
AV
35
36
37def adduser(args):
38 #TODO: Lets trust admins this do not validate Emails :)
8781ddb5 39 commands_util.setup_app(args)
96108ea2 40
4285fc67 41 args.username = unicode(commands_util.prompt_if_not_set(args.username, "Username:"))
7d98005a
MUS
42 args.password = commands_util.prompt_if_not_set(args.password, "Password:",True)
43 args.email = commands_util.prompt_if_not_set(args.email, "Email:")
44
8781ddb5
CAW
45 db = mg_globals.database
46 users_with_username = \
44082b12
RE
47 db.User.query.filter_by(
48 username=args.username.lower()
49 ).count()
96108ea2 50
8781ddb5 51 if users_with_username:
f9d93c0e 52 print(u'Sorry, a user with that name already exists.')
96108ea2 53
8781ddb5
CAW
54 else:
55 # Create the user
56 entry = db.User()
e49b7e02
BP
57 entry.username = six.text_type(args.username.lower())
58 entry.email = six.text_type(args.email)
9c2c9be7 59 entry.pw_hash = auth.gen_password_hash(args.password)
dfd66b78 60 default_privileges = [
9d6e453f 61 db.Privilege.query.filter(
62 db.Privilege.privilege_name==u'commenter').one(),
63 db.Privilege.query.filter(
64 db.Privilege.privilege_name==u'uploader').one(),
65 db.Privilege.query.filter(
66 db.Privilege.privilege_name==u'reporter').one(),
67 db.Privilege.query.filter(
68 db.Privilege.privilege_name==u'active').one()
69 ]
6bba33d7 70 entry.all_privileges = default_privileges
b39d1f23 71 entry.save()
96108ea2 72
f9d93c0e 73 print(u"User created (and email marked as verified)")
96108ea2
AV
74
75
76def makeadmin_parser_setup(subparser):
77 subparser.add_argument(
8781ddb5 78 'username',
96108ea2 79 help="Username to give admin level")
96108ea2
AV
80
81
82def makeadmin(args):
8781ddb5 83 commands_util.setup_app(args)
96108ea2 84
8781ddb5 85 db = mg_globals.database
96108ea2 86
44082b12 87 user = db.User.query.filter_by(
e49b7e02 88 username=six.text_type(args.username.lower())).one()
8781ddb5 89 if user:
3fb96fc9 90 user.all_privileges.append(
9d6e453f 91 db.Privilege.query.filter(
92 db.Privilege.privilege_name==u'admin').one()
93 )
8781ddb5 94 user.save()
f9d93c0e 95 print(u'The user is now Admin')
8781ddb5 96 else:
f9d93c0e 97 print(u'The user doesn\'t exist')
96108ea2
AV
98
99
100def changepw_parser_setup(subparser):
101 subparser.add_argument(
8781ddb5 102 'username',
96108ea2
AV
103 help="Username used to login")
104 subparser.add_argument(
8781ddb5 105 'password',
96108ea2 106 help="Your NEW supersecret word to login")
96108ea2
AV
107
108
109def changepw(args):
8781ddb5 110 commands_util.setup_app(args)
96108ea2 111
8781ddb5 112 db = mg_globals.database
96108ea2 113
44082b12 114 user = db.User.query.filter_by(
e49b7e02 115 username=six.text_type(args.username.lower())).one()
8781ddb5 116 if user:
9c2c9be7 117 user.pw_hash = auth.gen_password_hash(args.password)
8781ddb5 118 user.save()
f9d93c0e 119 print(u'Password successfully changed')
8781ddb5 120 else:
f9d93c0e 121 print(u'The user doesn\'t exist')
8c7ba963
LLN
122
123
124def deleteuser_parser_setup(subparser):
125 subparser.add_argument(
126 'username',
127 help="Username to delete")
128
129
130def deleteuser(args):
131 commands_util.setup_app(args)
132
133 db = mg_globals.database
134
135 user = db.User.query.filter_by(
136 username=unicode(args.username.lower())).one()
137 if user:
138 user.delete()
f6bad0eb 139 print('The user %s has been deleted' % args.username)
8c7ba963 140 else:
f6bad0eb 141 print('The user %s doesn\'t exist' % args.username)