It's 2012 all up in here
[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
96108ea2
AV
18from mediagoblin.auth import lib as auth_lib
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 = \
43 db.User.find({
243c3843 44 'username': args.username.lower(),
8781ddb5 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)
9047b254 55 entry.pw_hash = auth_lib.bcrypt_gen_password_hash(args.password)
7a3d00ec 56 entry.status = u'active'
4facc7a0 57 entry.email_verified = True
8781ddb5 58 entry.save(validate=True)
96108ea2 59
64201d12 60 print "User created (and email marked as verified)"
96108ea2
AV
61
62
63def makeadmin_parser_setup(subparser):
64 subparser.add_argument(
8781ddb5 65 'username',
96108ea2 66 help="Username to give admin level")
96108ea2
AV
67
68
69def makeadmin(args):
8781ddb5 70 commands_util.setup_app(args)
96108ea2 71
8781ddb5 72 db = mg_globals.database
96108ea2 73
243c3843 74 user = db.User.one({'username': unicode(args.username.lower())})
8781ddb5 75 if user:
bec591d8 76 user.is_admin = True
8781ddb5
CAW
77 user.save()
78 print 'The user is now Admin'
79 else:
80 print 'The user doesn\'t exist'
96108ea2
AV
81
82
83def changepw_parser_setup(subparser):
84 subparser.add_argument(
8781ddb5 85 'username',
96108ea2
AV
86 help="Username used to login")
87 subparser.add_argument(
8781ddb5 88 'password',
96108ea2 89 help="Your NEW supersecret word to login")
96108ea2
AV
90
91
92def changepw(args):
8781ddb5 93 commands_util.setup_app(args)
96108ea2 94
8781ddb5 95 db = mg_globals.database
96108ea2 96
243c3843 97 user = db.User.one({'username': unicode(args.username.lower())})
8781ddb5 98 if user:
9047b254 99 user.pw_hash = auth_lib.bcrypt_gen_password_hash(args.password)
8781ddb5
CAW
100 user.save()
101 print 'Password successfully changed'
102 else:
103 print 'The user doesn\'t exist'