Skip to content

Commit

Permalink
intern: rename ipadress to url
Browse files Browse the repository at this point in the history
  • Loading branch information
PiTrem committed Apr 15, 2024
1 parent 6cc4cfc commit 4e4f66a
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 95 deletions.
21 changes: 11 additions & 10 deletions app/api/chemotion/third_party_app_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,12 @@ def cache_key_for_encoded_token(payload)

desc 'create new third party app entry'
params do
requires :IPAddress, type: String, desc: 'The IPAddress in order to redirect to the app.'
requires :url, type: String, desc: 'The url in order to redirect to the app.'
requires :name, type: String, desc: 'name of third party app. User will chose correct app based on names.'
end
post '/new_third_party_app' do
declared(params, include_missing: false)
ThirdPartyApp.create!(IPAddress: params[:IPAddress], name: params[:name])
ThirdPartyApp.create!(url: params[:url], name: params[:name])
status 201
rescue ActiveRecord::RecordInvalid
error!('Unauthorized. User has to be admin.', 401)
Expand All @@ -152,13 +152,13 @@ def cache_key_for_encoded_token(payload)
desc 'update a third party app entry'
params do
requires :id, type: String, desc: 'The id of the app which should be updated'
requires :IPAddress, type: String, desc: 'The IPAddress in order to redirect to the app.'
requires :url, type: String, desc: 'The url in order to redirect to the app.'
requires :name, type: String, desc: 'name of third party app. User will chose correct app based on names.'
end
post '/update_third_party_app' do
declared(params, include_missing: false)
entry = ThirdPartyApp.find(params[:id])
entry.update!(IPAddress: params[:IPAddress], name: params[:name])
entry.update!(url: params[:url], name: params[:name])
status 201
rescue ActiveRecord::RecordInvalid
error!('Unauthorized. User has to be admin.', 401)
Expand Down Expand Up @@ -197,7 +197,7 @@ def cache_key_for_encoded_token(payload)
end
get 'IP' do
tpa = ThirdPartyApp.find_by(name: params[:name])
return tpa.IPAddress if tpa
return tpa.url if tpa

error_msg = "Third party app with ID: #{id} not found"
{ error: error_msg }
Expand All @@ -211,22 +211,23 @@ def cache_key_for_encoded_token(payload)
desc 'create token for use in download public_api'
params do
requires :attID, type: String, desc: 'Attachment ID'
requires :userID, type: String, desc: 'User ID'
requires :nameThirdPartyApp, type: String, desc: 'name of the third party app'
end
get 'Token' do
cache_key = "token/#{params[:attID]}/#{params[:userID]}/#{params[:nameThirdPartyApp]}"
payload = { attID: params[:attID], userID: params[:userID], nameThirdPartyApp: params[:nameThirdPartyApp] }
app = ThirdPartyApp.find_by(name: params[:nameThirdPartyApp])
cache_key = "token/#{params[:attID]}/#{current_user.id}/#{app.id}"
payload = { attID: params[:attID], userID: current_user.id, nameThirdPartyApp: params[:nameThirdPartyApp] }
cached_token = encode_token(payload, params[:nameThirdPartyApp])
Rails.cache.write(cache_key, cached_token, expires_in: 48.hours)
cached_token.token
address = app.url
"#{address}?token=#{cached_token.token}&url=#{CGI.escape(Rails.application.config.root_url)}"
end
end

resource :names do
desc 'Find all names of all third party app'
get 'all' do
ThirdPartyApp.all_names
ThirdPartyApp.pluck :name
end
end
end
Expand Down
11 changes: 1 addition & 10 deletions app/models/third_party_app.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
# frozen_string_literal: true

class ThirdPartyApp < ApplicationRecord
def self.all_names
return nil if ThirdPartyApp.count.zero?

entries = ThirdPartyApp.all
names = []
entries.each do |e|
names << e.name
end
names
end
validates :name, presence: true, uniqueness: true, length: { maximum: 100 }
end
81 changes: 31 additions & 50 deletions app/packs/src/apps/admin/ThirdPartyApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ export default class ThirdPartyApp extends React.Component {
});
}

new(name, IPAddress) {
new(name, url) {
ThirdPartyAppFetcher.newThirdPartyApp(
name,
IPAddress)
url)
.then((result) => {
if (result.error) {
this.setState({ messageNewThirdPartyAppModal: result.error });
Expand All @@ -77,11 +77,11 @@ export default class ThirdPartyApp extends React.Component {
return true;
}

edit(name, IPAddress) {
edit(name, url) {
return ThirdPartyAppFetcher.editThirdPartyApp(
this.state.currentID,
name,
IPAddress)
url)
.then((result) => {
if (result.error) {
return this.thirdPartyApps().then((res) => {
Expand Down Expand Up @@ -152,38 +152,28 @@ export default class ThirdPartyApp extends React.Component {
}

checkInput(name, ip) {
return new Promise((resolve, reject) => {

if (name.length < 1) {
this.setState({
errorMessageNewTPA: "name is shorter than 1 character"
});
reject();
return false;
}

if ((ip.slice(0, 7) != "http://") &&
(ip.slice(0, 8) != "https://")) {
this.setState({
errorMessageNewTPA: "Begin of ip address has to be http:// or https://"
});
reject();
return false;
}

ThirdPartyAppFetcher.isNameUnique(name)
.then((result) => {
const message = JSON.parse(result).message
if (message == "Name is not unique") {
this.setState({
errorMessageNewTPA: "name is not unique"
});
reject();
} else {
resolve();
}
})

})

if (this.state.thirdPartyAppNames.includes(name)) {
this.setState({
errorMessageNewTPA: "Name already exists"
});
return false;
}
return true;
}

getThirdPartyAppNames() {
Expand All @@ -207,7 +197,7 @@ export default class ThirdPartyApp extends React.Component {
.then((result) => {
this.setState({
currentName: result.name,
currentIP: result.IPAddress,
currentIP: result.url,
currentID: key
})
});
Expand Down Expand Up @@ -239,25 +229,21 @@ export default class ThirdPartyApp extends React.Component {


renderEditThirdPartyAppModal() {

let nameRef = null;
let IPAddressRef = null;
let urlRef = null;

const handleEdit = () => {

const IPAddress = IPAddressRef.value;
const url = urlRef.value;
const name = nameRef.value;
this.checkInput(name, IPAddress)
.then(() => {
this.edit(name, IPAddress).then(() => {
if (this.checkInput(name, url)) {
this.edit(name, url)
.then(() => {
this.getThirdPartyAppNames();
this.closeEditThirdPartyAppModal();
this.thirdPartyApps();
});
})
.catch(() => {
})

})
.catch(() => {});
}
}

const handleNameChange = (event) => {
Expand Down Expand Up @@ -296,7 +282,7 @@ export default class ThirdPartyApp extends React.Component {
IP address:
</Col>
<Col sm={9}>
<FormControl type="text" name="IP address" value={this.state.currentIP} onChange={handleIPChange} inputRef={(ref) => { IPAddressRef = ref; }} />
<FormControl type="text" name="IP address" value={this.state.currentIP} onChange={handleIPChange} inputRef={(ref) => { urlRef = ref; }} />
</Col>
</FormGroup>

Expand All @@ -320,21 +306,16 @@ export default class ThirdPartyApp extends React.Component {
renderMessageModal() {

let nameRef = null;
let IPAddressRef = null;
let urlRef = null;

const handleCreate = () => {

this.getThirdPartyAppNames();
const IPAddress = IPAddressRef.value;
const url = urlRef.value;
const name = nameRef.value;
this.checkInput(name, IPAddress)
.then(() => {
this.new(name, IPAddress);
this.closeNewThirdPartyAppModal();
})
.catch(() => {
});

if (this.checkInput(name, url)) {
this.new(name, url);
this.closeNewThirdPartyAppModal();
}
}

return (
Expand Down Expand Up @@ -362,7 +343,7 @@ export default class ThirdPartyApp extends React.Component {
IP address:
</Col>
<Col sm={9}>
<FormControl type="text" name="IP address" inputRef={(ref) => { IPAddressRef = ref; }} />
<FormControl type="text" name="IP address" inputRef={(ref) => { urlRef = ref; }} />
</Col>
</FormGroup>

Expand Down Expand Up @@ -435,7 +416,7 @@ export default class ThirdPartyApp extends React.Component {
</td>

<td>{entry.name}</td>
<td>{entry.IPAddress}</td>
<td>{entry.url}</td>
<td>{entry.id}</td>
</tr>
)
Expand Down
27 changes: 5 additions & 22 deletions app/packs/src/fetchers/ThirdPartyAppFetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,10 @@ export default class ThirdPartyAppFetcher {
.catch((errorMessage) => { console.log(errorMessage); });
}

static isNameUnique(name) {
const obj = {
name
};
return fetch('/api/v1/third_party_apps_administration/name_unique', {
credentials: 'same-origin',
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(obj)
}).then(response => response.json())
.then(json => json)
.catch((errorMessage) => { console.log(errorMessage); });
}

static newThirdPartyApp(name, IPAddress) {
static newThirdPartyApp(name, url) {
const obj = {
name,
IPAddress
url
};
return fetch('/api/v1/third_party_apps_administration/new_third_party_app', {
credentials: 'same-origin',
Expand All @@ -45,10 +28,10 @@ export default class ThirdPartyAppFetcher {
.catch((errorMessage) => { console.log(errorMessage); });
}

static editThirdPartyApp(id, name, IPAddress) {
static editThirdPartyApp(id, name, url) {
const obj = {
id: id,
IPAddress: IPAddress,
url: url,
name: name
};
return fetch('/api/v1/third_party_apps_administration/update_third_party_app', {
Expand Down Expand Up @@ -124,7 +107,7 @@ export default class ThirdPartyAppFetcher {
.catch((errorMessage) => { console.log(errorMessage); });
}

static fetchAttachmentToken(attID, userID, nameThirdPartyApp) {
static fetchAttachmentToken(attID, nameThirdPartyApp, userID) {
const obj = { attID, userID, nameThirdPartyApp };
const queryParams = new URLSearchParams(obj).toString();
const url = `/api/v1/third_party_apps/Token.json?${queryParams}`;
Expand Down
6 changes: 3 additions & 3 deletions db/migrate/20230213102539_create_third_party_apps.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
class CreateThirdPartyApps < ActiveRecord::Migration[6.1]
def change
create_table :third_party_apps do |t|
t.string :IPAddress
t.string :name
t.string :url
t.string :name, null: false, index: { unique: true }, limit: 100
t.timestamps
end
end
end
end

0 comments on commit 4e4f66a

Please sign in to comment.