@@ -40,6 +40,7 @@ def rotate(username = nil)
4040 say ( 'OK' , :green )
4141 else
4242 say ( 'No account(s) given' , :red )
43+ exit ( 1 )
4344 end
4445 end
4546
@@ -48,7 +49,7 @@ def rotate(username = nil)
4849 option :role , default : 'user'
4950 option :reattach , type : :boolean
5051 option :force , type : :boolean
51- desc 'add USERNAME' , 'Create a new user'
52+ desc 'create USERNAME' , 'Create a new user'
5253 long_desc <<-LONG_DESC
5354 Create a new user account with a given USERNAME and an
5455 e-mail address provided with --email.
@@ -65,7 +66,7 @@ def rotate(username = nil)
6566 the --force option to delete the old record and reattach the
6667 username to the new account anyway.
6768 LONG_DESC
68- def add ( username )
69+ def create ( username )
6970 account = Account . new ( username : username )
7071 password = SecureRandom . hex
7172 user = User . new ( email : options [ :email ] , password : password , admin : options [ :role ] == 'admin' , moderator : options [ :role ] == 'moderator' , confirmed_at : Time . now . utc )
@@ -98,19 +99,75 @@ def add(username)
9899 say ( key )
99100 say ( ' ' + error , :red )
100101 end
102+
103+ exit ( 1 )
101104 end
102105 end
103106
104- desc 'del USERNAME' , 'Delete a user'
107+ option :role
108+ option :email
109+ option :confirm , type : :boolean
110+ option :enable , type : :boolean
111+ option :disable , type : :boolean
112+ option :disable_2fa , type : :boolean
113+ desc 'modify USERNAME' , 'Modify a user'
114+ long_desc <<-LONG_DESC
115+ Modify a user account.
116+
117+ With the --role option, update the user's role to one of "user",
118+ "moderator" or "admin".
119+
120+ With the --email option, update the user's e-mail address. With
121+ the --confirm option, mark the user's e-mail as confirmed.
122+
123+ With the --disable option, lock the user out of their account. The
124+ --enable option is the opposite.
125+
126+ With the --disable-2fa option, the two-factor authentication
127+ requirement for the user can be removed.
128+ LONG_DESC
129+ def modify ( username )
130+ user = Account . find_local ( username ) &.user
131+
132+ if user . nil?
133+ say ( 'No user with such username' , :red )
134+ exit ( 1 )
135+ end
136+
137+ if options [ :role ]
138+ user . admin = options [ :role ] == 'admin'
139+ user . moderator = options [ :role ] == 'moderator'
140+ end
141+
142+ user . email = options [ :email ] if options [ :email ]
143+ user . disabled = false if options [ :enable ]
144+ user . disabled = true if options [ :disable ]
145+ user . otp_required_for_login = false if options [ :disable_2fa ]
146+ user . confirm if options [ :confirm ]
147+
148+ if user . save
149+ say ( 'OK' , :green )
150+ else
151+ user . errors . to_h . each do |key , error |
152+ say ( 'Failure/Error: ' , :red )
153+ say ( key )
154+ say ( ' ' + error , :red )
155+ end
156+
157+ exit ( 1 )
158+ end
159+ end
160+
161+ desc 'delete USERNAME' , 'Delete a user'
105162 long_desc <<-LONG_DESC
106163 Remove a user account with a given USERNAME.
107164 LONG_DESC
108- def del ( username )
165+ def delete ( username )
109166 account = Account . find_local ( username )
110167
111168 if account . nil?
112169 say ( 'No user with such username' , :red )
113- return
170+ exit ( 1 )
114171 end
115172
116173 say ( "Deleting user with #{ account . statuses_count } , this might take a while..." )
@@ -182,9 +239,56 @@ def cull
182239 end
183240 end
184241
242+ option :all , type : :boolean
243+ option :domain
244+ desc 'refresh [USERNAME]' , 'Fetch remote user data and files'
245+ long_desc <<-LONG_DESC
246+ Fetch remote user data and files for one or multiple accounts.
247+
248+ With the --all option, all remote accounts will be processed.
249+ Through the --domain option, this can be narrowed down to a
250+ specific domain only. Otherwise, a single remote account must
251+ be specified with USERNAME.
252+
253+ All processing is done in the background through Sidekiq.
254+ LONG_DESC
255+ def refresh ( username = nil )
256+ if options [ :domain ] || options [ :all ]
257+ queued = 0
258+ scope = Account . remote
259+ scope = scope . where ( domain : options [ :domain ] ) if options [ :domain ]
260+
261+ scope . select ( :id ) . reorder ( nil ) . find_in_batches do |accounts |
262+ Maintenance ::RedownloadAccountMediaWorker . push_bulk ( accounts . map ( &:id ) )
263+ queued += accounts . size
264+ end
265+
266+ say ( "Scheduled refreshment of #{ queued } accounts" , :green , true )
267+ elsif username . present?
268+ username , domain = username . split ( '@' )
269+ account = Account . find_remote ( username , domain )
270+
271+ if account . nil?
272+ say ( 'No such account' , :red )
273+ exit ( 1 )
274+ end
275+
276+ Maintenance ::RedownloadAccountMediaWorker . perform_async ( account . id )
277+ say ( 'OK' , :green )
278+ else
279+ say ( 'No account(s) given' , :red )
280+ exit ( 1 )
281+ end
282+ end
283+
185284 private
186285
187286 def rotate_keys_for_account ( account , delay = 0 )
287+ if account . nil?
288+ say ( 'No such account' , :red )
289+ exit ( 1 )
290+ end
291+
188292 old_key = account . private_key
189293 new_key = OpenSSL ::PKey ::RSA . new ( 2048 ) . to_pem
190294 account . update ( private_key : new_key )
0 commit comments