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 aac906c
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 96 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
1 change: 1 addition & 0 deletions app/api/chemotion/ui_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class UiAPI < Grape::API
has_converter: converter_config.present?,
has_radar: radar_config.present?,
collector_address: collector_address.presence,
third_party_apps: ThirdPartyApp.all
}
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
41 changes: 41 additions & 0 deletions app/packs/src/apps/mydb/elements/list/AttachmentList.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
} from 'react-bootstrap';
import ImageAnnotationEditButton from 'src/apps/mydb/elements/details/researchPlans/ImageAnnotationEditButton';
import { values } from 'lodash';
import uuid from 'uuid';
import SpinnerPencilIcon from 'src/components/common/SpinnerPencilIcon';
import Dropzone from 'react-dropzone';
import Utils from 'src/utilities/Functions';
Expand Down Expand Up @@ -313,3 +314,43 @@ export const sortingAndFilteringUI = (
</div>
</div>
);

export const thirdPartyAppBtn = (attachment, options, defaultOption, onChangingSelection) => (
<div style={{ float: 'right' }}>
<select
id="dropdown"
className="button"
value={defaultOption[attachment.id] || ''}
onChange={event => onChangingSelection(attachment, event.target.value)}
>
<option value="">--Third Party Apps--</option>
{options.map((option) => (
<option key={uuid.v4()} value={option}>
{option}
</option>
))}
</select>
</div>

);

export const thirdPartyAppButton = (attachment, options, onSelection) => (
<Dropdown id={`dropdown-TPA-attachment${attachment.id}`} style={{ float: 'right' }}>
<Dropdown.Toggle style={{ height: '30px' }} bsSize="xs" bsStyle="primary">
<i className="fa fa-external-link " aria-hidden="true" />
</Dropdown.Toggle>
<Dropdown.Menu>
{options.map((option) => (
<MenuItem
key={uuid.v4()}
eventKey={option.id}
onClick={() => onSelection(attachment, option)}
//disabled={!isImageFile(attachment.filename) || attachment.isNew}
>
{option.name}
</MenuItem>
))}
</Dropdown.Menu>
</Dropdown>
);

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
4 changes: 3 additions & 1 deletion app/packs/src/stores/alt/stores/UIStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ class UIStore {
modalParams: {},
hasChemSpectra: false,
hasNmriumWrapper: false,
matrices: {}
matrices: {},
thirdPartyApps: [],
};

this.bindListeners({
Expand Down Expand Up @@ -120,6 +121,7 @@ class UIStore {
}

handleInitialize(result) {
console.log(result)
this.setState(result);
const { klasses } = result;
klasses?.forEach((klass) => {
Expand Down
Loading

0 comments on commit aac906c

Please sign in to comment.