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 default_privileges
= [
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()
66 entry
.all_privileges
= default_privileges
69 print "User created (and email marked as verified)"
72 def makeadmin_parser_setup(subparser
):
73 subparser
.add_argument(
75 help="Username to give admin level")
79 commands_util
.setup_app(args
)
81 db
= mg_globals
.database
83 user
= db
.User
.query
.filter_by(
84 username
=unicode(args
.username
.lower())).one()
86 user
.all_privileges
.append(
87 db
.Privilege
.query
.filter(
88 db
.Privilege
.privilege_name
==u
'admin').one()
91 print 'The user is now Admin'
93 print 'The user doesn\'t exist'
96 def changepw_parser_setup(subparser
):
97 subparser
.add_argument(
99 help="Username used to login")
100 subparser
.add_argument(
102 help="Your NEW supersecret word to login")
106 commands_util
.setup_app(args
)
108 db
= mg_globals
.database
110 user
= db
.User
.query
.filter_by(
111 username
=unicode(args
.username
.lower())).one()
113 user
.pw_hash
= auth
.gen_password_hash(args
.password
)
115 print 'Password successfully changed'
117 print 'The user doesn\'t exist'
120 def deleteuser_parser_setup(subparser
):
121 subparser
.add_argument(
123 help="Username to delete")
126 def deleteuser(args
):
127 commands_util
.setup_app(args
)
129 db
= mg_globals
.database
131 user
= db
.User
.query
.filter_by(
132 username
=unicode(args
.username
.lower())).one()
135 print 'The user %s has been deleted' % args
.username
137 print 'The user %s doesn\'t exist' % args
.username