1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
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.
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.
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/>.
17 from mediagoblin
.gmg_commands
import util
as commands_util
18 from mediagoblin
import auth
19 from mediagoblin
import mg_globals
21 def adduser_parser_setup(subparser
):
22 subparser
.add_argument(
24 help="Username used to login")
25 subparser
.add_argument(
27 help="Your supersecret word to login, beware of storing it in bash history")
28 subparser
.add_argument(
30 help="Email to receive notifications")
34 #TODO: Lets trust admins this do not validate Emails :)
35 commands_util
.setup_app(args
)
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:")
41 db
= mg_globals
.database
42 users_with_username
= \
43 db
.User
.query
.filter_by(
44 username
=args
.username
.lower()
47 if users_with_username
:
48 print u
'Sorry, a user with that name already exists.'
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()
68 entry
.all_privileges
= default_privileges
71 print "User created (and email marked as verified)"
74 def makeadmin_parser_setup(subparser
):
75 subparser
.add_argument(
77 help="Username to give admin level")
81 commands_util
.setup_app(args
)
83 db
= mg_globals
.database
85 user
= db
.User
.query
.filter_by(
86 username
=unicode(args
.username
.lower())).one()
89 user
.all_privileges
.append(
90 db
.Privilege
.query
.filter(
91 db
.Privilege
.privilege_name
==u
'admin').one()
94 print 'The user is now Admin'
96 print 'The user doesn\'t exist'
99 def changepw_parser_setup(subparser
):
100 subparser
.add_argument(
102 help="Username used to login")
103 subparser
.add_argument(
105 help="Your NEW supersecret word to login")
109 commands_util
.setup_app(args
)
111 db
= mg_globals
.database
113 user
= db
.User
.query
.filter_by(
114 username
=unicode(args
.username
.lower())).one()
116 user
.pw_hash
= auth
.gen_password_hash(args
.password
)
118 print 'Password successfully changed'
120 print 'The user doesn\'t exist'