#!/usr/bin/env ruby # Amahi Home Server # Copyright (C) 2007-2010 Amahi # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License v3 # (29 June 2007), as published in the COPYING file. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # file COPYING for more details. # # You should have received a copy of the GNU General Public # License along with this program; if not, write to the Amahi # team at http://www.amahi.org/ under "Contact Us." require 'optparse' options = { :environment => (ENV['RAILS_ENV'] || "production").dup } options[:verbose] = true options[:unadmin] = false username = nil ARGV.clone.options do |opts| script_name = File.basename($0) opts.banner = "Usage: #{$0} [options] username newpassword" opts.separator "" opts.on("-u", "--unadmin", "Make it also not admin (required to be able to reset)") { |v| options[:unadmin] = true } opts.on("-v", "--verbose", "Make it verbose") { |v| options[:verbose] = true } opts.separator "" opts.on("-h", "--help", "Show this help message.") { $stderr.puts opts; exit } opts.order! { |o| username ||= o } rescue retry end password = nil puts ARGV.inspect username=ARGV[0] password=ARGV[1] unless username and password $stderr.puts "Usage: #{$0} [options] username password" exit 1 end ENV["RAILS_ENV"] = options[:environment] RAILS_ENV.replace(options[:environment]) if defined?(RAILS_ENV) APP_PATH = File.expand_path('../../config/application', __FILE__) require File.expand_path('../../config/boot', __FILE__) require APP_PATH Rails.application.require_environment! # ignore options[:verbose] for now u = User.find_by_login username unless u $stderr.puts "ERROR: Cannot find user with username '#{username}' in your server. Exiting." exit 1 end $stdout.puts "Resetting password for #{username} ... " u.crypted_password = nil u.password_salt = nil u.admin = false if options[:unadmin] u.password = u.password_confirmation = password unless u.save $stdout.puts u.errors.full_messages[0] exit 1 end $stdout.puts "done." $stdout.puts "*********************************************************" $stdout.puts " IMPORTANT: THE PASSWORD IS NOT ASKED TO BE CHANGED " $stdout.puts " *UNLESS* THERE ARE *NO* ADMIN USERS IN THE SYSTEM " $stdout.puts " USE THE -u OPTION TO MAKE A USER NOT-ADMIN " $stdout.puts "*********************************************************"