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