Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Api
- class AdminController < Api::BaseController
- #before_filter :authenticate_user!
- include ApplicationHelper
- def get_users
- render json: { data: Users.all }
- end
- def list_users
- authorize! :admin, :message => 'Not authorized as an administrator.'
- users = Array.new
- User.all.each do |user|
- roles = Array.new
- if user.visibility_class == "Group"
- user["location"] = LocationGroup.where(:location_group_id => user.visibility_id).only(:name).first()
- end
- if user.visibility_class == "Location"
- user["location"] = Location.where(:location_id => user.visibility_id).only(:name).first()
- end
- if(!user['role_ids'].nil?)
- user['role_ids'].each do |r|
- role = Role.find(r) rescue nil
- if(!role.nil?)
- roles.push ({ id: role.id, name: role.name })
- end
- end
- user['role_ids'] = roles
- else
- user['role_ids'] = []
- end
- users.push user
- end
- render json: { data: users }
- end
- def add_user
- @user = User.new
- end
- def create_user
- authorize! :admin, :message => 'Not authorized as an administrator.'
- email = params["user"]["email"]
- generated_password = Devise.friendly_token.first(12)
- begin
- default_widgets_setting = Setting.where(:name => 'default_widgets').first
- if !default_widgets_setting.nil?
- default_widgets = default_widgets_setting.value
- end
- user = User.create!(:email => email, :password => generated_password, :widget_control => default_widgets)
- #RegistrationMailer.welcome(user, generated_password).deliver
- render json: { status: :success, message: 'User was successfully created. Generated Password: ' + generated_password }
- rescue Exception => e1
- render json: { status: :error, message: 'User create Exception.' }
- end
- end
- def update_user
- user = params[:user]
- if user[:email] != @current_user[:email]
- render json: { error: 'Unauthorized'} , status: :unauthorized
- return
- else
- @userUpdated = User.where(email: user[:email]).first rescue nil
- if(@userUpdated.nil?)
- render json: {
- errors: {
- global: ['User is not available']
- }
- }, status: :unprocessable_entity
- return
- end
- if(!user[:current_password].blank?)
- if(@userUpdated.password != user[:current_password])
- render json: {
- errors: {
- global: ['Incorrect current password']
- }
- }, status: :unprocessable_entity
- return
- end
- if(user[:password] == user[:current_password])
- render json: {
- errors: {
- global: ['The new password cannot be the same of the current.']
- }
- }, status: :unprocessable_entity
- return
- end
- if(user[:password].blank?)
- render json: {
- errors: {
- global: ['Please fill the required field to change passwords']
- }
- }, status: :unprocessable_entity
- return
- end
- if(user[:password] != user[:password_confirmation])
- render json: {
- errors: {
- global: ['Password confirmation failed']
- }
- }, status: :unprocessable_entity
- return
- end
- @userUpdated.password = user[:password]
- end
- @userUpdated[:widget_control] = user[:widget_control]
- if @userUpdated.save
- render json: { status: 'success', user: @userUpdated }
- return
- else
- render json: {
- errors: {
- global: ['User failed to update']
- }
- }, status: :unprocessable_entity
- return
- end
- end
- end
- def assign_location_user
- #@users = User.all
- user = User.find(params["user_id"])
- user.visibility_class = params["visibility_class"]
- user.visibility_id = params["visibility_id"]
- if user.save
- render json: { status: 'success' }
- else
- render json: { status: 'error' }
- end
- end
- def get_user
- @user = User.find(params["userId"])
- if !@user.nil?
- render json: { status: 'success', user: @user }
- else
- render json: { status: 'error', message: 'User not found' }
- end
- end
- def add_user_role
- target_role = params[:role]
- user = User.find(params[:id])
- if !target_role.nil? && target_role == 'executive'
- user.add_role :executive
- end
- if !target_role.nil? && target_role == 'admin'
- user.add_role :admin
- end
- if !target_role.nil? && target_role == 'manager'
- user.add_role :manager
- end
- if !target_role.nil? && target_role == 'publisher'
- user.add_role :publisher
- end
- if !target_role.nil? && target_role == 'content creator'
- user.add_role :content_creator
- end
- if !target_role.nil? && target_role == 'content approver'
- user.add_role :content_approver
- end
- user.save
- render json: { status: 'success' }
- end
- def remove_user_role
- target_role = params[:role]
- user = User.find(params[:id])
- if !target_role.nil? && target_role == 'executive'
- user.remove_role :executive
- end
- if !target_role.nil? && target_role == 'admin'
- user.remove_role :admin
- end
- if !target_role.nil? && target_role == 'manager'
- user.remove_role :manager
- end
- if !target_role.nil? && target_role == 'publisher'
- user.remove_role :publisher
- end
- if !target_role.nil? && target_role == 'content creator'
- user.remove_role :content_creator
- end
- if !target_role.nil? && target_role == 'content approver'
- user.remove_role :content_approver
- end
- user.save
- render json: { status: 'success' }
- end
- def remove_user
- user = User.find(params[:id])
- if user == @current_user
- render json: { status: 'error', message: 'Unable to delete currently logged in user.' }
- else
- user.remove
- render json: { status: 'success' }
- end
- end
- def remote_status
- eyeq = Mongoid::Sessions.with_name(:core_data)
- coll = eyeq["remote_checks"]
- ds = coll.aggregate(
- {"$group" => {
- _id: {
- "loc_id" => "$loc_id",
- "place_id" => "$place_id",
- "mac" => "$mac",
- "e_ip" => "$e_ip",
- "i_ip" => "$i_ip",
- "hostname" => "$hostname"},
- capture_time: {"$max" => "$capture_time" }}},
- {"$sort" => {"capture_time" => -1}},
- {"$group" => {
- _id: {
- "loc_id" => "$_id.loc_id",
- "place_id" => "$_id.place_id"},
- "mac" => {"$first" => "$_id.mac"},
- "e_ip" => {"$first" => "$_id.e_ip"},
- "i_ip" => {"$first" => "$_id.i_ip"},
- "hostname" => {"$first" => "$_id.hostname"},
- "capture_time" => {"$first" => "$capture_time"}}},
- {"$sort" => {"_id.loc_id" => 1, "_id.place_id" => 1}}
- )
- @remote_devices = Array.new
- ds.each do |r|
- placement = Placement.where(:placement_id => r["_id"]["place_id"]).first
- if !placement.nil?
- location = Location.where(:location_id => placement.location_id).first
- if !r["capture_time"].nil?
- last_ts = r["capture_time"].in_time_zone('America/Chicago')
- else
- last_ts = nil
- end
- device = {
- :capture_time => last_ts,
- :trafficmap => get_last_timestamp(placement.placement_id, "trafficmap_all"),
- :demographics => get_last_timestamp(placement.placement_id, "demographics"),
- :engage => get_last_timestamp(placement.placement_id, "engagements"),
- :feedbacks => get_last_timestamp(placement.placement_id, "feedbacks"),
- :location => location.label + ' [' + location.location_id.to_s + ']',
- :location_active => location.active,
- :placement => placement.label + ' [' + placement.placement_id.to_s + ']',
- :placement_active => placement.active,
- :hostname => r["hostname"],
- :external_ip => r["e_ip"],
- :internal_ip => r["i_ip"],
- :wlan_mac => r["mac"],
- :location_id => location,
- :placement_id => placement,
- }
- @remote_devices.push(device)
- end
- end
- render json: { remote_devices: @remote_devices }
- end
- def get_last_timestamp(place_id, collection_name)
- eyeq = Mongoid::Sessions.with_name(:core_data)
- coll = eyeq[collection_name]
- ds = coll.find({"place_id"=>place_id}).sort({"local_time"=>-1}).limit(1)
- timestamp = ds.first
- if !timestamp.nil? && !timestamp["capture_time"].nil?
- return timestamp["capture_time"].in_time_zone('America/Chicago')
- else
- return nil
- end
- end
- def mac_history
- #access other database
- @start_date = GetStartDate(session['start_date'], params[:start_date])
- session['start_date'] = @start_date
- @end_date = GetAnyDate(session['end_date'], params[:end_date])
- session['end_date'] = @end_date
- @l_id_list = GetLocationList()
- default_login_location = 0;
- @l_id_list.each do |item|
- default_login_location = item[1]
- break
- end
- #(@l_id_list.count > 0) ? @l_id_list[0][1] : 1
- @l_id = GetParamSessionValues(session['l_id'], params[:l_id], default_login_location)
- session['l_id'] = @l_id
- macaddr = params[:mac]
- eyeq = Mongoid::Sessions.with_name(:core_data)
- coll = eyeq["remote_checks"]
- #db.remote_checks.find({"mac": /^c8:f7:33:ed:4d:ec$/i}).sort({"capture_time":1}).limit(100)
- @mac_history = coll.find({
- mac: macaddr,
- local_time: {
- "$gte" => DateToTime(@start_date),
- "$lt" => AddaDay(DateToTime(@end_date))}}
- ).sort({"capture_time" => -1}).limit(10)
- ds_mac_by_hour = coll.aggregate(
- {"$match" => {
- mac: macaddr,
- local_time: {
- "$gte" => DateToTime(@start_date),
- "$lt" => AddaDay(DateToTime(@end_date))}
- }},
- {"$group" => {
- _id:{
- "p_year" => {"$year" => "$local_time"},
- "p_month" => {"$month" => "$local_time"},
- "p_day" => {"$dayOfMonth" => "$local_time"},
- "p_hour" => {"$hour" => "$local_time"},
- },
- row_count:{"$sum" => 1}
- }},
- {"$sort" => {"_id.p_year" => 1, "_id.p_month" => 1, "_id.p_day" => 1, "_id.p_hour" => 1}}
- )
- #mac_by_hour_table = GoogleVisualr::DataTable.new
- mac_by_hour_table = Array.new
- #mac_by_hour_table.new_column('datetime', 'Hour' )
- #mac_by_hour_table.new_column('number', 'MAC Count')
- ds_mac_by_hour.each do |d|
- row_count = d["row_count"]
- time = Time.new(d["_id"]["p_year"], d["_id"]["p_month"], d["_id"]["p_day"], d["_id"]["p_hour"], 0, 0, '+00:00')
- mac_by_hour_table.push([time, row_count])
- end
- render json: { devices: mac_by_hour_table, mac_history: @mac_history }, root: false
- end
- def get_visibility_by_id
- visibilityClass = params["visibility_class"]
- if visibilityClass == "Group"
- location = Location.where(:location_id => params["visibility_id"])
- end
- if visibilityClass == "Location"
- location = LocationGroup.where(:location_id => params["visibility_id"])
- end
- render json: { data: location }, root: false
- end
- def manage_widgets
- default_widgets_setting = Setting.where(:name => 'default_widgets').first
- if (!default_widgets_setting.nil?)
- @widget_control = JSON.parse(default_widgets_setting.value)
- else
- @widget_control = JSON.parse('{}')
- end
- render json: { data: @widget_control }, root: false
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement