Merge remote-tracking branch 'refs/remotes/tsyesika/master'
[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)
7a3d00ec 56 entry.status = u'active'
4facc7a0 57 entry.email_verified = True
b39d1f23 58 entry.save()
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
44082b12
RE
74 user = db.User.query.filter_by(
75 username=unicode(args.username.lower())).one()
8781ddb5 76 if user:
bec591d8 77 user.is_admin = True
8781ddb5
CAW
78 user.save()
79 print 'The user is now Admin'
80 else:
81 print 'The user doesn\'t exist'
96108ea2
AV
82
83
84def changepw_parser_setup(subparser):
85 subparser.add_argument(
8781ddb5 86 'username',
96108ea2
AV
87 help="Username used to login")
88 subparser.add_argument(
8781ddb5 89 'password',
96108ea2 90 help="Your NEW supersecret word to login")
96108ea2
AV
91
92
93def changepw(args):
8781ddb5 94 commands_util.setup_app(args)
96108ea2 95
8781ddb5 96 db = mg_globals.database
96108ea2 97
44082b12
RE
98 user = db.User.query.filter_by(
99 username=unicode(args.username.lower())).one()
8781ddb5 100 if user:
9c2c9be7 101 user.pw_hash = auth.gen_password_hash(args.password)
8781ddb5
CAW
102 user.save()
103 print 'Password successfully changed'
104 else:
105 print 'The user doesn\'t exist'