rxcms-podio_plugin 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,153 @@
1
+ class ServicesController < ApplicationController
2
+ include PluginHelper
3
+
4
+ layout false
5
+
6
+ before_filter :get_current_user_role
7
+
8
+ # Load configuration items (MANDATORY, must be included)
9
+ APP_CONFIG = HashWithIndifferentAccess.new(YAML.load(File.read(File.expand_path('../../../config/podio/podio_config.yml', __FILE__))))
10
+
11
+ # Set current podio workspace
12
+ # Input
13
+ # Output
14
+ def set_current_podio_workspace
15
+ begin
16
+
17
+ if (@curUserRole == 'contentadmin' ||
18
+ @curUserRole == 'user' ||
19
+ @curUserRole == 'anonymous' ||
20
+ @curUserRole == 'loggedin')
21
+ raise 'unauthorized access'
22
+ end
23
+
24
+ space = params[:space]
25
+ metaId = Metadata.first({ :conditions => ['key = ? and sites_id = ?', APP_CONFIG[:PODIO_WORKSPACE], session[:accessible_appid]] });
26
+
27
+ if (!metaId.nil?)
28
+ Metadata.update(metaId, { :value => space })
29
+ else
30
+ Metadata.create({
31
+ :key => "currentWorkspace",
32
+ :value => space,
33
+ :mime => "plain/text",
34
+ :cat => "podio_config",
35
+ :sites_id => session[:accessible_appid]
36
+ }).save
37
+ end
38
+
39
+ render :json => { "status" => "success" }
40
+ rescue
41
+ render :json => { "status" => "failure", "message" => "Unable to set current podio workspace" }
42
+ end
43
+ end
44
+
45
+ # Get current podio workspace
46
+ # Input
47
+ # Output
48
+ def get_current_podio_workspace
49
+ begin
50
+
51
+ if (@curUserRole == 'contentadmin' ||
52
+ @curUserRole == 'user' ||
53
+ @curUserRole == 'anonymous' ||
54
+ @curUserRole == 'loggedin')
55
+ raise 'unauthorized access'
56
+ end
57
+
58
+ curWorkspace = Metadata.first({ :conditions => ['key = ? and sites_id = ?', APP_CONFIG[:PODIO_WORKSPACE], session[:accessible_appid]] })
59
+
60
+ if (!curWorkspace.nil?)
61
+ render :json => { "status" => "success", "data" => curWorkspace.value.to_s.strip }
62
+ else
63
+ render :json => { "status" => "failure" }
64
+ end
65
+ rescue
66
+ render :json => { "status" => "failure" }
67
+ end
68
+ end
69
+
70
+ # Get current podio service account user
71
+ # Input
72
+ # Output
73
+ def get_current_podio_user
74
+ begin
75
+
76
+ if (@curUserRole == 'contentadmin' ||
77
+ @curUserRole == 'user' ||
78
+ @curUserRole == 'anonymous' ||
79
+ @curUserRole == 'loggedin')
80
+ raise 'unauthorized access'
81
+ end
82
+
83
+ serviceAccount = Metadata.first({ :conditions => [
84
+ "key = ? and sites_id = ?", APP_CONFIG[:SERVICE_ACCOUNT_NAME], session[:accessible_appid]
85
+ ]})
86
+
87
+ if (!serviceAccount.nil?)
88
+ render :json => { "status" => "success", "data" => serviceAccount.value.strip}
89
+ else
90
+ render :json => { "status" => "failure" }
91
+ end
92
+ rescue
93
+ render :json => { "status" => "failure" }
94
+ end
95
+ end
96
+
97
+ # Update or create podio service account
98
+ # Input from POST['userObject'], POST['passObject']
99
+ # Output json string
100
+ def update_or_create_podio_service_account
101
+ SymmetricEncryption.load!
102
+
103
+ if (@curUserRole == 'contentadmin' ||
104
+ @curUserRole == 'user' ||
105
+ @curUserRole == 'anonymous' ||
106
+ @curUserRole == 'loggedin')
107
+ raise 'unauthorized access'
108
+ end
109
+
110
+ constServiceAccountInfoKey = APP_CONFIG[:SERVICE_ACCOUNT_NAME]
111
+ constServiceAccountPassKey = APP_CONFIG[:SERVICE_ACCOUNT_PASS]
112
+
113
+ podioUserObj = params[:userObject]
114
+ podioPassObj = params[:passObject]
115
+
116
+ begin
117
+ fPodioUser = Metadata.where("key = ? and sites_id = ?", constServiceAccountInfoKey, session[:accessible_appid]).first
118
+ fPodioPass = Metadata.where("key = ? and sites_id = ?", constServiceAccountPassKey, session[:accessible_appid]).first
119
+
120
+ if (!fPodioUser.nil?)
121
+ Metadata.update(fPodioUser.id, podioUserObj)
122
+ else
123
+ Metadata.create({
124
+ :key => constServiceAccountInfoKey,
125
+ :value => podioUserObj['value'],
126
+ :cat => podioUserObj['cat'],
127
+ :mime => podioUserObj['mime'],
128
+ :sites_id => session[:accessible_appid]
129
+ }).save!
130
+ end
131
+
132
+ if (!fPodioPass.nil?)
133
+ podioPassObj['value'] = SymmetricEncryption.encrypt(podioPassObj['value'])
134
+ Metadata.update(fPodioPass.id, podioPassObj)
135
+ else
136
+ Metadata.create({
137
+ :key => constServiceAccountPassKey,
138
+ :value => SymmetricEncryption.encrypt(podioPassObj['value']),
139
+ :cat => podioPassObj['cat'],
140
+ :mime => podioPassObj['mime'],
141
+ :sites_id => session[:accessible_appid]
142
+ }).save!
143
+ end
144
+
145
+ render :json => { :status => "success" }
146
+ rescue Exception => ex
147
+ render :json => { :status => "failure", :message => ex.message }
148
+ end
149
+ end
150
+
151
+ private
152
+
153
+ end
@@ -0,0 +1,120 @@
1
+ class SessionsController < ApplicationController
2
+ layout nil
3
+
4
+ # Load configuration items (MANDATORY, must be included)
5
+ APP_CONFIG = HashWithIndifferentAccess.new(YAML.load(File.read(File.expand_path('../../../config/podio/podio_config.yml', __FILE__))))
6
+
7
+ # Create_Session as user action
8
+ def create_session_as_user
9
+ begin
10
+ SymmetricEncryption.load!
11
+
12
+ constServiceAccountInfoKey = APP_CONFIG[:SERVICE_ACCOUNT_NAME]
13
+ constServiceAccountPassKey = APP_CONFIG[:SERVICE_ACCOUNT_PASS]
14
+
15
+ apiKey = Metadata.first({:conditions => ["key = ? and sites_id = ?", APP_CONFIG[:PODIO_API_KEY], session[:accessible_appid]]
16
+ })
17
+ secretKey = Metadata.first({:conditions => ["key = ? and sites_id = ?", APP_CONFIG[:PODIO_SECRET_KEY], session[:accessible_appid]]
18
+ })
19
+
20
+ # Get service user account information from database
21
+ usrName = Metadata.where("key = ? and sites_id = ?", constServiceAccountInfoKey, session[:accessible_appid]).first
22
+ usrPass = Metadata.where("key = ? and sites_Id = ?", constServiceAccountPassKey, session[:accessible_appid]).first
23
+
24
+ # API key, secret key, user and password credentials are required to proceed; if not, an error is raised
25
+ if (!usrName.nil? && !usrPass.nil? && !apiKey.nil? && !secretKey.nil?)
26
+
27
+ Podio.setup(
28
+ :api_url => 'https://api.podio.com',
29
+ :api_key => apiKey.value.strip,
30
+ :api_secret => secretKey.value.strip
31
+ )
32
+
33
+ # Authenticate using user ID
34
+ Podio.client.authenticate_with_credentials(usrName.value.strip, SymmetricEncryption.decrypt(usrPass.value.strip))
35
+
36
+ # Store authentication session variables
37
+ session[:podio_access_token] = Podio.client.oauth_token.access_token
38
+ session[:podio_refresh_token] = Podio.client.oauth_token.refresh_token
39
+
40
+ # Store credential hash as cookies
41
+ cookies.delete(:podio)
42
+ cookies[:podio] = Digest::SHA2.hexdigest("#{usrName.value.strip}#{usrPass.value.strip}")
43
+
44
+ if (!cookies[:url].nil? && !cookies[:url].empty?)
45
+ redirect_to cookies[:url].to_s
46
+ else
47
+ redirect_to "/"
48
+ end
49
+ else
50
+ raise
51
+ end
52
+
53
+ rescue Exception => ex
54
+
55
+ raise "#{ex.message}"
56
+
57
+ end
58
+ end
59
+
60
+ # End of Create_Session as user action
61
+ def change_session_as_user
62
+ begin
63
+ SymmetricEncryption.load!
64
+
65
+ constServiceAccountInfoKey = APP_CONFIG[:SERVICE_ACCOUNT_NAME]
66
+ constServiceAccountPassKey = APP_CONFIG[:SERVICE_ACCOUNT_PASS]
67
+
68
+ apiKey = Metadata.first({:conditions => ["key = ? and sites_id = ?", APP_CONFIG[:PODIO_API_KEY], session[:accessible_appid]]})
69
+ secretKey = Metadata.first({:conditions => ["key = ? and sites_id = ?", APP_CONFIG[:PODIO_SECRET_KEY], session[:accessible_appid]]})
70
+
71
+ # Get service user account information from database
72
+ usrName = Metadata.where("key = ? and sites_id = ?", constServiceAccountInfoKey, session[:accessible_appid]).first
73
+ usrPass = Metadata.where("key = ? and sites_id = ?", constServiceAccountPassKey, session[:accessible_appid]).first
74
+
75
+ if (!usrName.nil? && !usrPass.nil? && !apiKey.nil? && !secretKey.nil?)
76
+ Podio.setup(
77
+ :api_url => 'https://api.podio.com',
78
+ :api_key => apiKey.value.strip,
79
+ :api_secret => secretKey.value.strip
80
+ )
81
+
82
+ # Authenticate using user ID
83
+ Podio.client.authenticate_with_credentials(usrName.value.strip, SymmetricEncryption.decrypt(usrPass.value.strip))
84
+
85
+ # Store authentication session variables
86
+ session[:podio_access_token] = Podio.client.oauth_token.access_token
87
+ session[:podio_refresh_token] = Podio.client.oauth_token.refresh_token
88
+
89
+ # Store credential hash as cookies
90
+ cookies.delete(:podio)
91
+ cookies[:podio] = Digest::SHA2.hexdigest("#{usrName.value.strip}#{usrPass.value.strip}")
92
+
93
+ render :json => {:status => "success"}
94
+ else
95
+ render :json => {:status => "failure"}
96
+ end
97
+
98
+ rescue Exception => ex
99
+
100
+ render :json => {:status => "failure"}
101
+
102
+ end
103
+ end
104
+
105
+ # Success_Callback, will be called if a session is successfully created
106
+ # def success_callback
107
+
108
+ # render :text => 'Session created!'
109
+
110
+ # end
111
+
112
+ # Delete_Session action, will be called if a session is successfully cleared
113
+ # def delete_session
114
+
115
+ # session.clear
116
+ # render :text => 'Session cleared!'
117
+
118
+ # end
119
+
120
+ end
@@ -0,0 +1,30 @@
1
+ module PluginHelper
2
+
3
+ def current_user
4
+ if UserSession.find.nil?
5
+ @current_user ||= nil
6
+ else
7
+ @current_user ||= User.find(UserSession.find.record.id)
8
+ end
9
+ end
10
+
11
+ # Get current user's role
12
+ def get_current_user_role
13
+ @current_user = current_user
14
+ if (!@current_user.nil?)
15
+ siteId = session[:accessible_appid]
16
+ roleId = session[:accessible_roleid]
17
+
18
+ if (!siteId.nil? && !roleId.nil?)
19
+ userRole = Role.find(roleId)
20
+ @curUserRole = userRole.name
21
+ else
22
+ @curUserRole = 'loggedin'
23
+ end
24
+
25
+ else
26
+ @curUserRole = 'anonymous'
27
+ end
28
+ end
29
+
30
+ end
@@ -0,0 +1,29 @@
1
+ class AbstractApplication < Podio::Application
2
+
3
+ def self.get_apps_list_by_space(spaceid)
4
+ begin
5
+ Podio::Application.find_all_for_space(spaceid)
6
+ rescue
7
+ []
8
+ end
9
+ end
10
+
11
+ def self.test_authentication?(user, pass)
12
+ begin
13
+ Podio.client.authenticate_with_credentials(user, pass)
14
+ return true
15
+ rescue
16
+ return false
17
+ end
18
+ end
19
+
20
+ def self.app_exists?(appid)
21
+ begin
22
+ Podio::Application.find(appid)
23
+ return true
24
+ rescue
25
+ return false
26
+ end
27
+ end
28
+
29
+ end
@@ -0,0 +1,207 @@
1
+ class AbstractItem < Podio::Item
2
+ # To change this template use File | Settings | File Templates.
3
+
4
+ @@abbrErr = "PAI"
5
+
6
+ # Find all items in the app
7
+ def self.range(app_id, external_ids = [{}], attrs = {:order => "ASC", :field => "item_id", :limit => -1, :offset => -1}, filter_by = [{}])
8
+ #begin
9
+ #collection = self.find_by_filter_values(app_id, {}, { :limit => 1, :offset => 1 })
10
+ attrs[:order] = attrs[:order].nil? ? "ASC" : attrs[:order]
11
+ attrs[:field] = attrs[:field].nil? ? "item_id" : attrs[:field]
12
+ attrs[:limit] = (attrs[:limit].nil? || attrs[:limit] == -1) ? 30 : attrs[:limit]
13
+ attrs[:offset] = (attrs[:offset].nil? || attrs[:offset] == -1) ? 0 : attrs[:offset]
14
+
15
+ # Check the app if it exists?
16
+ if (AbstractApplication.app_exists?(app_id) == false)
17
+ raise "AbstractItem - #{@@abbrErr}001 The app doesn't exist or service account doesn't have access to it"
18
+ end
19
+
20
+ # Start getting data
21
+ collection = nil
22
+
23
+ begin
24
+ collection = self.find_by_filter_values(app_id, {}, {:limit => attrs[:limit], :offset => attrs[:offset]})
25
+ rescue
26
+ raise "AbstractItem - #{@@abbrErr}002 The access to the app was restricted"
27
+ end
28
+
29
+ data = collection[:all]
30
+
31
+ tArray = Array.new
32
+ tAttributes = Array.new
33
+ # Flag for getting all existent attributes
34
+ tAttributesFlag = true
35
+
36
+ data.each do |item|
37
+ tHash = Hash.new
38
+
39
+ # Populate items into a customized array of items
40
+ tHash['item_id'] = item.attributes[:item_id]
41
+ external_ids.each do |id|
42
+ if (id[:simple])
43
+ value = field_values_by_external_id(item, id[:external_id], {:simple => true})
44
+
45
+ if !value.nil?
46
+ tHash[id[:external_id]] = value
47
+ if (tAttributesFlag)
48
+ tAttributes << id[:external_id]
49
+ end
50
+ end
51
+ else
52
+ value = field_values_by_external_id(item, id[:external_id], {})
53
+
54
+ if !value.nil?
55
+ tHash[id[:external_id]] = value
56
+ if (tAttributesFlag)
57
+ tAttributes << id[:external_id]
58
+ end
59
+ end
60
+ end
61
+ end
62
+
63
+ # Set flag to false to prevent recursively adding fields
64
+ tAttributesFlag = false
65
+ tArray << tHash
66
+
67
+ end
68
+
69
+ #raise "#{tAttributes.count.to_s}"
70
+
71
+ # Change and specify datatypes if needed
72
+ tArray.each do |item|
73
+ tAttributes.each do |attr|
74
+ item[attr] = Float(item[attr]) rescue item[attr].to_s
75
+ end
76
+ end
77
+
78
+ # Sort items in the customized array of items
79
+ if (attrs[:order] == "ASC")
80
+ tArray.sort! { |a, b| a[attrs[:field]] <=> b[attrs[:field]] }
81
+ elsif (attrs[:order] == "DESC")
82
+ tArray.sort! { |a, b| a[attrs[:field]] <=> b[attrs[:field]] }
83
+ tArray.reverse!
84
+ end
85
+
86
+ tArray
87
+ #rescue Exception => ex
88
+ #raise 'Access denied, please check your podio access permission!'
89
+ #end
90
+ end
91
+
92
+ # Find all items based on query
93
+ # def self.all_by_query(app_id, query = {})
94
+ # if (query.empty?)
95
+ # raise "AbstractItem - #{@@abbrErr}003 Query must not be empty!"
96
+ # else
97
+ # collection = self.find_by_filter_values(app_id, query)
98
+ # collection[:all]
99
+ # end
100
+ # end
101
+
102
+ # Find all items with options and query
103
+ # def self.all_by_query(app_id, query = {}, options = {})
104
+ # if (query.empty?)
105
+ # raise "AbstractItem - #{@@abbrErr}003 Query must not be empty!"
106
+ # else
107
+ # collection = self.find_by_filter_values(app_id, query)
108
+ # collection[:all]
109
+ # end
110
+ # end
111
+
112
+ # Find a range of items with options and query
113
+ # def self.find_range_by_query(app_id, query = {}, options = {})
114
+ # if (query.empty?)
115
+ # raise "AbstractItem - #{@@abbrErr}003 Query must not be empty!"
116
+ # else
117
+ #
118
+ # end
119
+ # end
120
+
121
+ #
122
+ # COMMENT SECTION
123
+ #
124
+ # CONSTANTS
125
+ IS_LIKED = :is_liked
126
+ CREATED_BY = :created_by
127
+ CREATED_BY_TYPE = :type
128
+ CREATED_BY_AVATAR = :avatar
129
+ CREATED_BY_USER_ID = :user_id
130
+ CREATED_BY_IMAGE = :image
131
+ CREATED_BY_LAST_SEEN_ON = :last_seen_on
132
+ CREATED_BY_AVATAR_TYPE = :avatar_type
133
+ CREATED_BY_NAME = :name
134
+ CREATED_BY_ID = :id
135
+ CREATED_BY_AVATAR_ID = :avatar_id
136
+ CREATED_BY_URL = :url
137
+ VALUE = :value
138
+ CREATED_ON = :created_on
139
+ FILES = :files
140
+ COMMENT_ID = :comment_id
141
+ RICH_VALUE = :rich_value
142
+ LIKE_COUNT = :like_count
143
+ #
144
+ #
145
+ #
146
+
147
+ # Find all comments in an item
148
+ # def self.comments(item_id)
149
+ # Podio::Comment.find_all_for('item',item_id)
150
+ # end
151
+
152
+ # Get comment's attribute
153
+ # def self.comment_attribute(comment, attribute = :value, options = [])
154
+ # raw_comment = comment.attributes[attribute]
155
+ # if (options.empty?)
156
+ # begin
157
+ # raw_comment
158
+ # rescue
159
+ # raise "AbstractItem - #{@@abbrErr}004 Invalid Operation"
160
+ # end
161
+ # else
162
+ # begin
163
+ # tHash = Hash.new
164
+ # options.each do |t|
165
+ # tHash[t] = raw_comment[t]
166
+ # end
167
+ # tHash
168
+ # rescue
169
+ # raise "AbstractItem - #{@@abbrErr}004 Invalid Operation"
170
+ # end
171
+ # end
172
+ # end
173
+
174
+ # Get current database connection name
175
+ # def self.database_conn_name
176
+ # ActiveRecord::Base.connection.adapter_name.downcase
177
+ # end
178
+
179
+ protected
180
+
181
+ # Default methods
182
+ def self.field_values_by_external_id(item, external_id, options = {})
183
+ if item.attributes[:fields].present?
184
+ fields = item.attributes[:fields]
185
+ matched = false
186
+
187
+ fields.each do |field|
188
+ if (field['external_id'] == external_id)
189
+ values = field['values']
190
+ matched = true
191
+ if (options[:simple])
192
+ return values.first['value']
193
+ else
194
+ return values
195
+ end
196
+ end
197
+ end
198
+
199
+ if !matched
200
+ nil
201
+ end
202
+ else
203
+ nil
204
+ end
205
+ end
206
+
207
+ end