Permalink
Browse files

moves user folder structure to user model

  • Loading branch information...
MSchmidt committed Jun 3, 2010
2 parents f8a9eef + 131972c commit fe2f95462f592a4b786057a20a9653a00f14db03
View
@@ -4,8 +4,8 @@ doc/api
doc/app
db/*.sqlite3
config/database.yml
config/gccp.yml
config/initializers/session_store.rb
config/initializers/gccp.rb
*.*~
*~
nbproject
@@ -1,7 +1,6 @@
class DomainsController < ApplicationController
before_filter :is_admin, :except => [:index, :show, :edit, :update]
before_filter :check_for_unsaved
# GET /domains
# GET /domains.xml
@@ -61,7 +60,6 @@ def edit
# ADMIN only
def create
@domain = Domain.new(params[:domain])
@domain.saved_by = current_user
respond_to do |format|
if @domain.save
@@ -113,39 +111,4 @@ def destroy
format.xml { head :ok }
end
end
def folder_structure
folders = Domain.get_user_www_dir_structure
respond_to do |format|
format.html { redirect_to(domains_url) }
format.js { render :json => folders }
format.xml do
xml = Builder::XmlMarkup.new(:indent => 2)
xml.ul { build_xml_branch(folders, xml) }
render :xml => xml.target!
end
end
end
protected
def check_for_unsaved
@unsaved_domains = Domain.all(:conditions => { :saved => false, :saved_by => current_user })
@unsaved_domains_count = @unsaved_domains.count
end
def build_xml_branch(branch, xml)
branch.keys.sort.each do |directory|
if branch[directory].empty?
xml.li(directory)
else
xml.li(directory, :class => 'has_sub')
xml.li(:class => "is_sub #{directory}-sub") do
xml.ul do
build_xml_branch(branch[directory], xml)
end
end
end
end
end
end
@@ -33,9 +33,8 @@ def create
@user = User.new(params[:user])
respond_to do |format|
if @user.save
#Delayed::Job.enqueue User.find(@user.id)
@user.send_later(:write_config, @user.password)
if @user.save
#@user.send_later(:write_config, @user.password)
flash[:notice] = "User was successfully created. Please save the password: #{@user.password}"
format.html { redirect_to(users_url) }
format.xml { render :xml => @user, :status => :created, :location => @user }
@@ -55,7 +54,8 @@ def update
respond_to do |format|
if @user.update_attributes(params[:user])
if params[:user][:password] && params[:user][:password].any?
#@user.send_later(:update_config, @user.password, oldname)
unless params[:user][:password].blank?
flash[:notice] = "User was successfully updated. Please save the new password: #{@user.password}"
else
flash[:notice] = "User was successfully updated."
@@ -77,7 +77,8 @@ def update_password
@user = current_user
respond_to do |format|
if @user.valid_password?(params[:old_password]) && (params[:user][:password] == params[:user][:password_confirmation]) && params[:user][:password].any? && @user.update_attribute(:password, params[:user][:password])
if @user.valid_password?(params[:old_password]) && (params[:user][:password] == params[:user][:password_confirmation]) && params[:user][:password] && @user.update_attribute(:password, params[:user][:password])
@user.send_later(:update_config, @user.password)
flash[:notice] = 'User Password was successfully changed.'
format.html { redirect_to(user_root_url) }
format.xml { head :ok }
@@ -94,7 +95,7 @@ def update_db_password
respond_to do |format|
if @user.update_attribute(:dbpassword, params[:user][:dbpassword])
ConnectedDatabase.send_later(:change_user_password, :name => @user.name, :password => @user.dbpassword)
ConnectedDatabase.send_later(:change_user_password, :name => @user.username, :password => @user.dbpassword)
flash[:notice] = 'User DB Password was successfully changed.'
format.html { redirect_to(databases_url) }
format.xml { head :ok }
@@ -107,11 +108,42 @@ def update_db_password
def destroy
@user = User.find(params[:id])
@user.send_later(:destroy_config, @user.name) if @user.destroy
@user.destroy
respond_to do |format|
format.html { redirect_to(users_url) }
format.xml { head :ok }
end
end
def folder_structure
folders = User.get_user_www_dir_structure
respond_to do |format|
format.html { redirect_to(domains_url) }
format.js { render :json => folders }
format.xml do
xml = Builder::XmlMarkup.new(:indent => 2)
xml.ul { build_xml_branch(folders, xml) }
render :xml => xml.target!
end
end
end
protected
def build_xml_branch(branch, xml)
branch.keys.sort.each do |directory|
if branch[directory].empty?
xml.li(directory)
else
xml.li(directory, :class => 'has_sub')
xml.li(:class => "is_sub #{directory}-sub") do
xml.ul do
build_xml_branch(branch[directory], xml)
end
end
end
end
end
end
@@ -2,38 +2,45 @@ class ConnectedDatabase < ActiveRecord::Base
establish_connection :connected_database
def self.create_database(options={})
if options[:name]
unless options[:name].blank?
connection.execute "CREATE DATABASE `#{options[:name]}`;"
end
end
def self.create_user(options={})
if options[:name] && options[:password]
unless options[:name].blank? && options[:password].blank?
connection.execute "CREATE USER '#{options[:name]}'@'localhost' IDENTIFIED BY '#{options[:password]}';"
end
end
def self.grant_permission(options={})
if options[:dbname] && options[:user]
unless options[:dbname].blank? && options[:user].blank?
connection.execute "GRANT SELECT , INSERT , UPDATE , DELETE ON `#{options[:dbname]}`.* TO '#{options[:user]}'@'localhost';"
end
end
def self.destroy_database(options={})
if options[:name]
unless options[:name].blank?
connection.execute "DROP DATABASE `#{options[:name]}`;"
end
end
def self.destroy_user(options={})
if options[:name]
unless options[:name].blank?
connection.execute "DROP USER '#{options[:name]}'@'localhost';"
end
end
def self.change_user_password(options={})
if options[:name] && options[:password]
unless options[:name].blank? && options[:password].blank?
connection.execute "SET PASSWORD FOR '#{options[:name]}'@'localhost' = PASSWORD( '#{options[:password]}' );"
end
end
def self.rename_user(options={})
unless options[:name].blank? && options[:oldname].blank?
res = connection.execute("SELECT COUNT(*) FROM user WHERE User = '#{options[:name]}';")
connection.execute "RENAME USER '#{options[:oldname]}'@'localhost' TO '#{options[:name]}'@'localhost';" if res.fetch_row.to_i != 0
end
end
end
View
@@ -12,7 +12,7 @@ def perform
def write_config
dbname = self.name
username = self.user.name
username = self.user.username
userpwd = self.user.dbpassword
ConnectedDatabase::create_database(:name => dbname)
View
@@ -5,59 +5,42 @@ class Domain < ActiveRecord::Base
validates_uniqueness_of :fqdn
validates_length_of :fqdn, :minimum => 4
validates_presence_of :user_id
validates_presence_of :saved_by
def perform
write_config
end
def write_config
begin
servername = self.fqdn
serveralias = "www." + servername
email = self.user.email
folder = self.mount_point
php = self.php
rails = self.rails
servername = self.fqdn
serveralias = "www." + servername
email = self.user.email
path = File.join(self.user.userpath, self.mount_point)
php = self.php
rails = self.rails
vhost_template = ERB.new(read_template('apache2_vhost.conf')).result(binding)
index_template = ERB.new(read_template('index.html')).result(binding)
vhost_template = ERB.new(read_template('apache2_vhost.conf')).result(binding)
index_template = ERB.new(read_template('index.html')).result(binding)
Dir.chdir(VHOST_TARGET_DIR) #definend in config/initializers/gccp.rb
File.open('gccp_' + servername, "w") do |f|
f.write(vhost_template)
end
system("a2ensite gccp_#{servername}")
system("/etc/init.d/apache2 reload")
File.open(File.join(VHOST_TARGET_DIR,VHOST_PREFIX + servername), "w") do |f|
f.write(vhost_template)
end
Dir.mkdir(WWW_DIR + folder)
Dir.chdir(WWW_DIR + folder)
Dir.mkdir(path) unless File.exists?(path)
if rails
#adduser deploy
#gem install mongrel_cluster
#a2enmod rewrite
#a2enmod proxy
#a2enmod proxy_http
#a2enmod proxy_balancer
system("chown deploy:deploy #{WWW_DIR + folder}")
system("mkdir -p current/public")
system("echo 'hello world' > current/public/index.html")
Dir.mkdir("/etc/mongrel_cluster")
system("chown deploy:deploy /etc/mongrel_cluster")
end
indexfile = "index.html"
indexfile = "index.php" if php
indexfile = File.join(path, "index.html")
indexfile = File.join(path, "index.php") if php
unless File.exists?(indexfile)
File.open(indexfile, "w") do |f|
f.write(index_template)
end
end
system("a2ensite #{VHOST_PREFIX+servername}")
system("/etc/init.d/apache2 reload")
rescue Errno::ENOENT
puts "No such directory"
end
end
def read_template(file)
@@ -71,11 +54,11 @@ def destroy_config
servername = self.fqdn
folder = self.mount_point
system("a2dissite gccp_#{servername}")
system("a2dissite #{VHOST_PREFIX+servername}")
Dir.chdir(VHOST_TARGET_DIR) #definend in config/initializers/gccp.rb
File.delete('gccp_' + servername) if File.exist?('gccp_'+servername)
system("rm -rf #{WWW_DIR + folder}")
vhost_path = File.join(VHOST_TARGET_DIR, VHOST_PREFIX + servername)
File.delete(vhost_path) if File.exist?(vhost_path)
#system("rm -rf " + File.join(WWW_DIR, self.mount_point))
system("/etc/init.d/apache2 reload")
@@ -84,20 +67,4 @@ def destroy_config
end
end
end
def self.get_user_www_dir_structure
folders = Hash.new{ |h,k| h[k] = Hash.new &h.default_proc }
#Dir.chdir(WWW_DIR)
Dir.glob(File.join("**", "**")) do |path|
if File.directory?(path)
sub = folders
path.split('/').each do |dir|
sub = sub[dir]
end
end
end
return folders
end
end
Oops, something went wrong.

0 comments on commit fe2f954

Please sign in to comment.