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 __future__
import print_function
23 from mediagoblin
.db
.models
import LocalUser
24 from mediagoblin
.gmg_commands
import util
as commands_util
25 from mediagoblin
import auth
26 from mediagoblin
import mg_globals
28 def adduser_parser_setup(subparser
):
29 subparser
.add_argument(
31 help="Username used to login")
32 subparser
.add_argument(
34 help="Your supersecret word to login, beware of storing it in bash history")
35 subparser
.add_argument(
37 help="Email to receive notifications")
41 #TODO: Lets trust admins this do not validate Emails :)
42 commands_util
.setup_app(args
)
44 args
.username
= six
.text_type(commands_util
.prompt_if_not_set(args
.username
, "Username:"))
45 args
.password
= commands_util
.prompt_if_not_set(args
.password
, "Password:",True)
46 args
.email
= commands_util
.prompt_if_not_set(args
.email
, "Email:")
48 db
= mg_globals
.database
49 users_with_username
= \
50 db
.LocalUser
.query
.filter(
51 LocalUser
.username
==args
.username
.lower()
54 if users_with_username
:
55 print(u
'Sorry, a user with that name already exists.')
60 entry
= db
.LocalUser()
61 entry
.username
= six
.text_type(args
.username
.lower())
62 entry
.email
= six
.text_type(args
.email
)
63 entry
.pw_hash
= auth
.gen_password_hash(args
.password
)
64 default_privileges
= [
65 db
.Privilege
.query
.filter(
66 db
.Privilege
.privilege_name
==u
'commenter').one(),
67 db
.Privilege
.query
.filter(
68 db
.Privilege
.privilege_name
==u
'uploader').one(),
69 db
.Privilege
.query
.filter(
70 db
.Privilege
.privilege_name
==u
'reporter').one(),
71 db
.Privilege
.query
.filter(
72 db
.Privilege
.privilege_name
==u
'active').one()
74 entry
.all_privileges
= default_privileges
77 print(u
"User created (and email marked as verified).")
80 def makeadmin_parser_setup(subparser
):
81 subparser
.add_argument(
83 help="Username to give admin level",
88 commands_util
.setup_app(args
)
90 db
= mg_globals
.database
92 user
= db
.LocalUser
.query
.filter(
93 LocalUser
.username
==args
.username
.lower()
96 user
.all_privileges
.append(
97 db
.Privilege
.query
.filter(
98 db
.Privilege
.privilege_name
==u
'admin').one()
101 print(u
'The user %s is now an admin.' % args
.username
)
103 print(u
'The user %s doesn\'t exist.' % args
.username
)
107 def changepw_parser_setup(subparser
):
108 subparser
.add_argument(
110 help="Username used to login",
112 subparser
.add_argument(
114 help="Your NEW supersecret word to login")
118 commands_util
.setup_app(args
)
120 db
= mg_globals
.database
122 user
= db
.LocalUser
.query
.filter(
123 LocalUser
.username
==args
.username
.lower()
126 user
.pw_hash
= auth
.gen_password_hash(args
.password
)
128 print(u
'Password successfully changed for user %s.' % args
.username
)
130 print(u
'The user %s doesn\'t exist.' % args
.username
)
134 def deleteuser_parser_setup(subparser
):
135 subparser
.add_argument(
137 help="Username to delete",
141 def deleteuser(args
):
142 commands_util
.setup_app(args
)
144 db
= mg_globals
.database
146 user
= db
.LocalUser
.query
.filter(
147 LocalUser
.username
==args
.username
.lower()
151 print('The user %s has been deleted.' % args
.username
)
153 print('The user %s doesn\'t exist.' % args
.username
)