PageRenderTime 27ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/app/controllers/mac_addresses_controller.rb

https://github.com/toyokazu/mac_address_manager
Ruby | 183 lines | 145 code | 17 blank | 21 comment | 14 complexity | 760bdd203e13cac4651301deabf88030 MD5 | raw file
  1. require 'csv'
  2. require 'ipaddr'
  3. class MacAddressesController < ApplicationController
  4. before_filter CASClient::Frameworks::Rails::Filter
  5. before_filter :authorize
  6. # GET /mac_addresses
  7. # GET /mac_addresses.xml
  8. # for admin_user
  9. # GET /groups/1/mac_addresses
  10. # GET /groups/1/mac_addresses.xml
  11. def index
  12. if !params["group_id"].nil? && admin_user?
  13. begin
  14. @group = Group.find(params["group_id"])
  15. rescue ActiveRecord::RecordNotFound => error
  16. flash[:notice] = 'Specified group does not exists.'
  17. redirect_back_or_default and return
  18. end
  19. end
  20. @mac_addresses = MacAddress.all(gen_cond(params["group_id"]))
  21. @mac_addresses = @mac_addresses.sort {|a, b| IPAddr.new((a.ipv4_addr.nil? || a.ipv4_addr.empty?) ? "255.255.255.255" : a.ipv4_addr) <=> IPAddr.new((b.ipv4_addr.nil? || b.ipv4_addr.empty?) ? "255.255.255.255" : b.ipv4_addr)}
  22. respond_to do |format|
  23. format.html # index.html.erb
  24. format.xml { render :xml => @mac_addresses }
  25. format.csv do
  26. CSV::Writer.generate(output = "", "\t") do |csv|
  27. @mac_addresses.each do |mac_addr|
  28. ip_addr = nil
  29. if (mac_addr.ipv4_addr.nil? || mac_addr.ipv4_addr.empty?)
  30. ip_addr = mac_addr.ipv6_addr
  31. else
  32. ip_addr = mac_addr.ipv4_addr
  33. end
  34. csv << [mac_addr.hostname, mac_addr.mac_addr, mac_addr.description, ip_addr, mac_addr.vlan_id]
  35. end
  36. end
  37. send_data(output, :type => 'text/csv')
  38. end
  39. end
  40. end
  41. # GET /mac_addresses/1
  42. # GET /mac_addresses/1.xml
  43. # for admin_user
  44. # GET /groups/1/mac_addresses
  45. # GET /groups/1/mac_addresses.xml
  46. def show
  47. if !params["group_id"].nil? && admin_user?
  48. begin
  49. @group = Group.find(params["group_id"])
  50. rescue ActiveRecord::RecordNotFound => error
  51. flash[:notice] = 'Specified group does not exists.'
  52. redirect_back_or_default and return
  53. end
  54. end
  55. begin
  56. @mac_address = MacAddress.find(params[:id], gen_cond(params["group_id"]))
  57. rescue ActiveRecord::RecordNotFound => error
  58. flash[:notice] = 'You do not have a permission.'
  59. redirect_back_or_default and return
  60. end
  61. respond_to do |format|
  62. format.html # show.html.erb
  63. format.xml { render :xml => @mac_address }
  64. end
  65. end
  66. # GET /mac_addresses/new
  67. # GET /mac_addresses/new.xml
  68. def new
  69. @mac_address = MacAddress.new(:group_id => current_user.default_group_id)
  70. respond_to do |format|
  71. format.html # new.html.erb
  72. format.xml { render :xml => @mac_address }
  73. end
  74. end
  75. # GET /mac_addresses/upload
  76. def upload
  77. respond_to do |format|
  78. format.html # upload.html.erb
  79. end
  80. end
  81. # GET /mac_addresses/1/edit
  82. def edit
  83. begin
  84. @mac_address = MacAddress.find(params[:id], gen_cond)
  85. rescue ActiveRecord::RecordNotFound => error
  86. flash[:notice] = 'You do not have a permission.'
  87. redirect_back_or_default and return
  88. end
  89. end
  90. # POST /mac_addresses
  91. # POST /mac_addresses.xml
  92. def create
  93. @mac_address = MacAddress.new(params[:mac_address])
  94. respond_to do |format|
  95. if @mac_address.save
  96. flash[:notice] = 'MacAddress was successfully created.'
  97. format.html { redirect_to(@mac_address) }
  98. format.xml { render :xml => @mac_address, :status => :created, :location => @mac_address }
  99. else
  100. format.html { render :action => "new" }
  101. format.xml { render :xml => @mac_address.errors, :status => :unprocessable_entity }
  102. end
  103. end
  104. end
  105. # POST /mac_addresses/update_all
  106. def update_all
  107. client = Rinda::Client.new('update', :ts_uri => ts_uri, :key => session[:session_id])
  108. repeat_count = 3
  109. until client.worker.lock(current_user.default_group.id)
  110. sleep(1)
  111. repeat_count = repeat_count - 1
  112. if repeat_count == 0
  113. flash[:notice] = 'Update daemon seems to be busy now. Please wait a few minutes and try again.'
  114. redirect_back_or_default and return
  115. end
  116. end
  117. client.update_and_unlock_request(current_user.default_group.id, params[:file][:csv])
  118. flash[:notice] = 'Your update request was submitted. Please wait a few minitues for the database to be updated.'
  119. respond_to do |format|
  120. format.html { redirect_to(root_path) }
  121. format.xml { render :xml => :ok }
  122. end
  123. end
  124. # PUT /mac_addresses/1
  125. # PUT /mac_addresses/1.xml
  126. def update
  127. begin
  128. @mac_address = MacAddress.find(params[:id], gen_cond)
  129. rescue ActiveRecord::RecordNotFound => error
  130. flash[:notice] = 'You do not have a permission.'
  131. redirect_back_or_default and return
  132. end
  133. respond_to do |format|
  134. if @mac_address.update_attributes(params[:mac_address])
  135. flash[:notice] = 'MacAddress was successfully updated.'
  136. format.html { redirect_to(@mac_address) }
  137. format.xml { head :ok }
  138. else
  139. format.html { render :action => "edit" }
  140. format.xml { render :xml => @mac_address.errors, :status => :unprocessable_entity }
  141. end
  142. end
  143. end
  144. # DELETE /mac_addresses/1
  145. # DELETE /mac_addresses/1.xml
  146. def destroy
  147. begin
  148. @mac_address = MacAddress.find(params[:id], gen_cond)
  149. rescue ActiveRecord::RecordNotFound => error
  150. flash[:notice] = 'You do not have a permission.'
  151. redirect_back_or_default and return
  152. end
  153. @mac_address.destroy
  154. respond_to do |format|
  155. format.html { redirect_to(mac_addresses_url) }
  156. format.xml { head :ok }
  157. end
  158. end
  159. def gen_cond(group_id = nil)
  160. if admin_user?
  161. return {} if group_id.nil?
  162. return {:conditions => {:group_id => group_id}}
  163. end
  164. {:conditions => {:group_id => current_user.default_group.id}}
  165. end
  166. end