Class: DbmsConnectionController

Inherits:
ApplicationController
  • Object
show all
Includes:
DbmsPluginHelper
Defined in:
app/controllers/dbms_connection_controller.rb

Constant Summary collapse

APP_CONFIG =

Load configuration items (MANDATORY, must be included)

HashWithIndifferentAccess.new(YAML.load(File.read(File.expand_path('../../../config/dbms/dbms_config.yml', __FILE__))))

Instance Method Summary collapse

Methods included from DbmsPluginHelper

#current_user, #get_current_user_role

Instance Method Details

#addStoredQueryObject



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'app/controllers/dbms_connection_controller.rb', line 186

def addStoredQuery
  SymmetricEncryption.load!

  begin

    # Check access
    if (@curUserRole == 'contentadmin' ||
        @curUserRole == 'user' ||
        @curUserRole == 'loggedin' ||
        @curUserRole == 'anonymous')
      raise 'unauthorized access'
    end

    # if (Metadata.all(:conditions => ["key = ?", params[:key]]).length > 0)
    #  raise "key is duplicated"
    # end

    if (!Metadata.first({ :conditions => ['key = ?', params[:key]]}).nil?)
      raise "key is duplicated"
    end

    # Check for value duplications
    storedQueryValues = Metadata.all(:conditions => ["cat = ? and sites_id = ?", APP_CONFIG[:DBMS_CAT_STORED_QUERY], session[:accessible_appid]])
    storedQueryValues.each do |t|
      valueJsonObject = ActiveSupport::JSON.decode(SymmetricEncryption.decrypt(t.value))

      if valueJsonObject['sql'] == params[:value]
        raise "value is duplicated"
      end
    end

    # If all is fine, create the stored query
    Metadata.create({
                        :key => params[:key],
                        :value => SymmetricEncryption.encrypt({ :sql => params[:value], :enabled => false, :language => 'none' }.to_json),
                        :cat => APP_CONFIG[:DBMS_CAT_STORED_QUERY],
                        :mime => "text/sql",
                        :sites_id => session[:accessible_appid]
                    })

    render :json => {:status => "success"}
  rescue Exception => ex
    render :json => {:status => "failure", :message => ex.message}
  end
end

#deleteStoredQueryObject



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'app/controllers/dbms_connection_controller.rb', line 273

def deleteStoredQuery
  begin

    # Check access
    if (@curUserRole == 'contentadmin' ||
        @curUserRole == 'user' ||
        @curUserRole == 'loggedin' ||
        @curUserRole == 'anonymous')
      raise 'unauthorized access'
    end

    Metadata.find_by_id(params[:id]).destroy

    render :json => {:status => "success"}
  rescue Exception => ex
    render :json => {:status => "failure", :message => ex.message}
  end
end

#executeSQLObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'app/controllers/dbms_connection_controller.rb', line 91

def executeSQL
  begin

    # Check access
    if (@curUserRole == 'contentadmin' ||
        @curUserRole == 'user' ||
        @curUserRole == 'loggedin' ||
        @curUserRole == 'anonymous')
      raise 'unauthorized access'
    end

    if (ActiveRecord::Base.connection != Database.connection)

      resultSet = []
      begin
        Database.connection.execute("BEGIN")
        resultSet = Database.connection.select_all(params[:sql])
        # Must put ROLLBACK here because Postgres adapter doesn't throw any exception when update or delete commands are executed
        Database.connection.execute("ROLLBACK")

        # Parse columns
        columns = Array.new
        if resultSet.length > 0
          # Take this first one
          firstResult = resultSet.first
          columns = firstResult.keys
        end

        render :json => {:status => "success", :data => {:columns => columns, :result => resultSet}}
      rescue NoMethodError
        Database.connection.execute("ROLLBACK")
        render :json => {:status => "success", :data => {:columns => [], :result => []}}
      rescue ActiveRecord::StatementInvalid => dberr
        render :json => { :status => "failure", :data => {:columns => [], :result => []}, :message => dberr.message }
      end

    else
      render :json => {:status => "failure", :data => {:columns => [], :result => []}, :message => "connect not established"}
    end
  rescue Exception => ex
    render :json => {:status => "failure", :data => {:columns => [], :result => []}, :message => "command executed, no result set returned"}
  end
end

#getConnectionStatusObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/controllers/dbms_connection_controller.rb', line 42

def getConnectionStatus
  begin

    # Check access
    if (@curUserRole == 'contentadmin' ||
        @curUserRole == 'user' ||
        @curUserRole == 'loggedin' ||
        @curUserRole == 'anonymous')
      raise 'unauthorized access'
    end

    if (ActiveRecord::Base.connection != Database.connection)
      status = Database.connection.active?

      if (status)
        render :json => {:status => "success", :data => Database.connection.current_database}
      else
        render :json => {:status => "failure", :message => "database unavailable"}
      end
    else
      render :json => {:status => "failure", :message => "connection unavailable"}
    end
  rescue Exception => ex
    render :json => {:status => "failure", :message => ex.message}
  end
end

#getListOfStoredQueriesObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'app/controllers/dbms_connection_controller.rb', line 135

def getListOfStoredQueries
  begin

    # Check access
    if (@curUserRole == 'contentadmin' ||
        @curUserRole == 'user' ||
        @curUserRole == 'loggedin' ||
        @curUserRole == 'anonymous')
      raise 'unauthorized access'
    end

    storedQueries = Metadata.all({ :conditions => ['cat = ? and sites_id = ?', APP_CONFIG[:DBMS_CAT_STORED_QUERY], session[:accessible_appid]] })

    tArray = Array.new
    storedQueries.each do |t|
      tHash = Hash.new

      tHash[:id] = t.id
      tHash[:key] = t.key
      tHash[:value] = SymmetricEncryption.decrypt(t.value)

      tArray << tHash
    end

    render :json => {:status => "success", :data => tArray}
  rescue Exception => ex
    render :json => {:status => "failure", :message => ex.message}
  end
end

#getListOfTablesObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/controllers/dbms_connection_controller.rb', line 69

def getListOfTables
  begin

    # Check access
    if (@curUserRole == 'contentadmin' ||
        @curUserRole == 'user' ||
        @curUserRole == 'loggedin' ||
        @curUserRole == 'anonymous')
      raise 'unauthorized access'
    end

    if (ActiveRecord::Base.connection != Database.connection)
      tables = Database.connection.tables
      render :json => {:status => "success", :data => tables}
    else
      render :json => {:status => "success", :message => []}
    end
  rescue Exception => ex
    render :json => {:status => "failure", :message => ex.message}
  end
end

#getOneStoredQueryInfosObject



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'app/controllers/dbms_connection_controller.rb', line 165

def getOneStoredQueryInfos
  SymmetricEncryption.load!

  begin

    # Check access
    if (@curUserRole == 'contentadmin' ||
        @curUserRole == 'user' ||
        @curUserRole == 'loggedin' ||
        @curUserRole == 'anonymous')
      raise 'unauthorized access'
    end

    storedQuery = Metadata.first({ :conditions => [ 'id = ? and cat = ? and sites_id = ?', params[:id], params[:cat], session[:accessible_appid] ]})

    render :json => {:status => "success", :data => {:id => storedQuery.id ,:key => storedQuery.key, :value => SymmetricEncryption.decrypt(storedQuery.value)}}
  rescue Exception => ex
    render :json => {:status => "failure", :message => ex.message}
  end
end

#makeNewConnectionObject

Controller Actions



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/controllers/dbms_connection_controller.rb', line 11

def makeNewConnection
  SymmetricEncryption.load!

  begin

    # Check access
    if (@curUserRole == 'contentadmin' ||
        @curUserRole == 'user' ||
        @curUserRole == 'loggedin' ||
        @curUserRole == 'anonymous')
      raise 'unauthorized access'
    end

    connectionResult = false

    if (params[:type] == "sqlite")
      connectionResult = Database.connect_sqlite(params[:database])
    else
      connectionResult = Database.connect_sql(params[:type], params[:host], params[:port], params[:user], SymmetricEncryption.encrypt(params[:password]), params[:database])
    end

    if (connectionResult)
      render :json => {:status => "success"}
    else
      render :json => {:status => "failure"}
    end
  rescue Exception => ex
    render :json => {:status => "failure", :message => ex.message}
  end
end

#updateStoredQueryObject



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'app/controllers/dbms_connection_controller.rb', line 232

def updateStoredQuery
  SymmetricEncryption.load!

  begin

    # Check access
    if (@curUserRole == 'contentadmin' ||
        @curUserRole == 'user' ||
        @curUserRole == 'loggedin' ||
        @curUserRole == 'anonymous')
      raise 'unauthorized access'
    end

    # select that id first
    updateObj = Metadata.find(params[:id])

    if (!updateObj.nil?)
      updateObjData = ActiveSupport::JSON.decode(SymmetricEncryption.decrypt(updateObj.value))
      if (!params[:value].nil? && params[:enabled].nil? && params[:language].nil?)
        Metadata.update(params[:id], {
            :value => SymmetricEncryption.encrypt({ :sql => params[:value], :enabled => updateObjData['enabled'], :language => updateObjData['language'] }.to_json)
        });
      elsif (params[:value].nil? && !params[:enabled].nil? && params[:language].nil?)
        Metadata.update(params[:id], {
            :value => SymmetricEncryption.encrypt({ :sql => updateObjData['sql'] , :enabled => params[:enabled] == "true" ? true : false, :language => updateObjData['language'] }.to_json)
        });
      elsif (params[:value].nil? && params[:enabled].nil? && !params[:language].nil?)
        Metadata.update(params[:id], {
            :value => SymmetricEncryption.encrypt({ :sql => updateObjData['sql'] , :enabled => updateObjData['enabled'], :language => params[:language] }.to_json)
        });
      else
        # Unimplemented
      end
    end

    render :json => {:status => "success"}
  rescue Exception => ex
    render :json => {:status => "failure", :message => ex.message}
  end
end