diff --git a/src/lib/api/apigateway.proto b/src/lib/api/apigateway.proto index aa2f10466..f55413bba 100644 --- a/src/lib/api/apigateway.proto +++ b/src/lib/api/apigateway.proto @@ -10,6 +10,7 @@ message GatewayLoginResult { bool is_valid = 1; string reason = 2; string token = 3; + bool can_create = 4; } message GatewayCallHeader { int32 request_id = 1; diff --git a/src/lib/api/services/UserService.proto b/src/lib/api/services/UserService.proto index f5aff089a..f6a59ecb4 100644 --- a/src/lib/api/services/UserService.proto +++ b/src/lib/api/services/UserService.proto @@ -47,6 +47,7 @@ message AuthenticateLoginResponse { string reason = 2; AuthToken token = 3; uint32 user_id = 4; + bool can_create = 5; } message AuthenticateTokenRequest {AuthToken token = 1;} message AuthenticateTokenResponse {bool is_valid = 1; string reason = 2; User user = 3;} diff --git a/src/server/sparkd/http/ApiGateway.cpp b/src/server/sparkd/http/ApiGateway.cpp index 213786c60..ec889dc80 100644 --- a/src/server/sparkd/http/ApiGateway.cpp +++ b/src/server/sparkd/http/ApiGateway.cpp @@ -46,6 +46,7 @@ struct ApiGatewayImpl { if (logon_response.result()->is_valid()) { resp.mutable_logon()->set_is_valid(true); resp.mutable_logon()->set_token(logon_response.result()->token().token()); + resp.mutable_logon()->set_can_create(logon_response.result()->can_create()); } else { resp.mutable_logon()->set_reason(logon_response.result()->reason()); diff --git a/src/server/sparkd/user/UserServiceServer.cpp b/src/server/sparkd/user/UserServiceServer.cpp index e90d6644f..60fcbe60d 100644 --- a/src/server/sparkd/user/UserServiceServer.cpp +++ b/src/server/sparkd/user/UserServiceServer.cpp @@ -66,6 +66,10 @@ void UserServiceServer::authenticate_login(const api::user::AuthenticateLoginReq resp.set_is_valid(user.is_active()); resp.set_user_id(user.id()); resp.mutable_token()->CopyFrom(api::auth::generate_token(user)); + + api::user::TokenPayload payload; + api::auth::decode_token(resp.token().token(), &payload); + resp.set_can_create(payload.wheel() || user.can_create()); respond(resp); return; } diff --git a/src/web/api/src/ServiceContext.ts b/src/web/api/src/ServiceContext.ts index 6e17094ab..19f155865 100644 --- a/src/web/api/src/ServiceContext.ts +++ b/src/web/api/src/ServiceContext.ts @@ -65,6 +65,7 @@ class PrivateContext { this.ws.onmessage = e => this.onmessage(e) console.log("Authenticated") this.sc.token = response.logon.token + this.sc.can_create = response.logon.can_create resolve(this.sc) } else { @@ -121,7 +122,8 @@ class PrivateContext { export class ServiceContext { next_id : number send : (msg)=>void - token : string + token: string + can_create: boolean outstanding : { [request:number]:{ resolve : (buffer)=>void, reject: (msg)=>void } } constructor(send : (msg)=>void) { diff --git a/src/web/ui/src/Accounts/State.fs b/src/web/ui/src/Accounts/State.fs index 4b43374d7..b1925752e 100644 --- a/src/web/ui/src/Accounts/State.fs +++ b/src/web/ui/src/Accounts/State.fs @@ -33,6 +33,7 @@ let init (context:ServiceContext.ServiceContext) = SelectedClipLimits = [] SelectedPositionLimits = [] token = context.token + can_create = context.can_create }, Access.getAdminData admin context.token UpdateAdminData NoAdminData let existingSelection (scratch:BTSAccount) (previousScratch:BTSAccount) (data:AdminData) = @@ -189,8 +190,10 @@ let update msg (model:Model) : Model * Cmd = | true -> mdl, Cmd.ofExistingPromise (deleteAccount model.admin account model ) id (box>>ErrorOccured) | false -> mdl, Cmd.Empty | AddAccount -> + match model.can_create with // Create a new scratch account. Send to server after user has filled in details - { model with previousScratch = model.scratch; scratch = { emptyAccount with name = "New Account" } }, Cmd.ofMsg <| SelectTab General + | true-> { model with previousScratch = model.scratch; scratch = { emptyAccount with name = "New Account" } }, Cmd.ofMsg <| SelectTab General + | false-> model, Cmd.Empty | SaveScratch -> match isNewAccount model.scratch with | true -> model, Cmd.ofExistingPromise (createAccount model.admin model.scratch model ) id (box>>ErrorOccured) diff --git a/src/web/ui/src/Accounts/Types.fs b/src/web/ui/src/Accounts/Types.fs index 6b64c0cd9..519a9c44a 100644 --- a/src/web/ui/src/Accounts/Types.fs +++ b/src/web/ui/src/Accounts/Types.fs @@ -104,4 +104,5 @@ type Model = { OptionsMenuOpen:string ConfirmModal: string * Msg token: string + can_create: bool } \ No newline at end of file diff --git a/src/web/ui/src/Accounts/View.fs b/src/web/ui/src/Accounts/View.fs index de892c05b..f30daf7c5 100644 --- a/src/web/ui/src/Accounts/View.fs +++ b/src/web/ui/src/Accounts/View.fs @@ -12,6 +12,8 @@ open System open bts.co2 +let accountIsSelected (initialState:BTSAccount) (scratch:BTSAccount) = scratch <> initialState + let selectedAccount model = match model.adminData with | Some data -> @@ -72,7 +74,7 @@ let generalView model dispatch = Fields.text "Name" account.name [] (AccountName >> Update >> dispatch) Fields.text "Description" account.description [] (Description >> Update >> dispatch) Fields.checkbox "Can Trade Into Account" "Unchecking this stops all users from entering orders into the account" account.isActive [] (IsActive >> Update >> dispatch) - (if State.isNewAccount account then modal else span[][]) + (if (State.isNewAccount account && model.can_create) then modal else span[][]) accountControls model dispatch ] @@ -730,8 +732,8 @@ let exchangeSettings model dispatch = let tabContent model dispatch = div [Key (string model.scratch.id); ClassName "is-tab-content"] [ // (match model.tab with - (match model.tab, (selectedAccount model ) with - | _ , None -> p [Style [ Color "Red" ]] [ str "Select an Account" ] + (match model.tab, (accountIsSelected model.scratch Access.Accounts.empty) with + | _ , false -> p [Style [ Color "Red" ]] [ str "Select an Account" ] | General, _ -> generalView model dispatch | LimitsTab, _ -> LimitsView model dispatch | ConnectionsTab, _ -> ConnectionsView model dispatch diff --git a/src/web/ui/src/Users/State.fs b/src/web/ui/src/Users/State.fs index 38d3e140b..d8cf26947 100644 --- a/src/web/ui/src/Users/State.fs +++ b/src/web/ui/src/Users/State.fs @@ -115,6 +115,7 @@ let init (context:ServiceContext.ServiceContext) : Model * Cmd = OptionsMenuOpen = "" ConfirmModal = "", (Msg.SwitchTab Tab.General) token = context.token + can_create = context.can_create }, getAdminData admin context.token let rec removeUser (user:BTSUser) (userList:BTSUser list) = @@ -169,7 +170,7 @@ let deleteUser (admin:IAdminService) (user:BTSUser) (model:Model) = promise { return ErrorOccured (sprintf "Error deleting user: %s" result.errorText) } -let createUser (admin:IAdminService) (user:BTSUser) = promise { +let createUser (admin:IAdminService) (user:BTSUser) (model:Model)= promise { let args : UserCreateAPIArgument = { parentGroupId = user.parentGroupId name = user.name @@ -178,10 +179,10 @@ let createUser (admin:IAdminService) (user:BTSUser) = promise { company = user.company licenseData = user.licenseData licenseEntitlements = user.licenseEntitlements - } + } let! result = admin.create_user args if result.isSuccess then - return! saveUser admin { user with id = result.newId } + return! saveUser admin { user with id = result.newId } model else return ErrorOccured (sprintf "Error creating user: %s" result.errorText) } @@ -300,7 +301,9 @@ let update msg (model:Users.Types.Model) : Model * Cmd = | false -> mdl, Cmd.Empty | AddUser -> - { model with PreviousScratchUser = model.scratchUser; scratchUser = { defaultScratchUser with name = "New User" }}, Cmd.ofMsg (SwitchTab General) + match model.can_create with + | true -> { model with PreviousScratchUser = model.scratchUser; scratchUser = { defaultScratchUser with name = "New User" }}, Cmd.ofMsg (SwitchTab General) + | false -> model, Cmd.Empty | CancelAddUser -> { model with scratchUser = { model.PreviousScratchUser with name = "New User" }}, Cmd.ofMsg (SwitchTab General) | SwitchTab tb -> @@ -311,7 +314,7 @@ let update msg (model:Users.Types.Model) : Model * Cmd = | SaveUser -> // match on id = 0 to see if it's a new user match model.scratchUser.id with - | 0u -> { model with PreviousScratchUser = model.scratchUser}, Cmd.ofExistingPromise (createUser model.admin {model.scratchUser with name = model.scratchUser.name.ToLower() }) id (box>>ErrorOccured) + | 0u -> { model with PreviousScratchUser = model.scratchUser}, Cmd.ofExistingPromise (createUser model.admin {model.scratchUser with name = model.scratchUser.name.ToLower() } model) id (box>>ErrorOccured) | _ -> model, Cmd.ofExistingPromise (saveUser model.admin {model.scratchUser with name = model.scratchUser.name.ToLower() } model) id (box>>ErrorOccured) | ErrorOccured msg -> //Browser.console.log msg diff --git a/src/web/ui/src/Users/Types.fs b/src/web/ui/src/Users/Types.fs index 8c33cf2f9..1341230f5 100644 --- a/src/web/ui/src/Users/Types.fs +++ b/src/web/ui/src/Users/Types.fs @@ -61,6 +61,7 @@ type Model = { OptionsMenuOpen: string ConfirmModal: string * Msg token: string + can_create: bool } diff --git a/src/web/ui/src/Users/View.fs b/src/web/ui/src/Users/View.fs index ca79619fd..85b8fc0c4 100644 --- a/src/web/ui/src/Users/View.fs +++ b/src/web/ui/src/Users/View.fs @@ -38,6 +38,8 @@ let saveButton (model:Users.Types.Model) dispatch = let enabled = if dirty then Button.Enabled else Button.Disabled let flavor = if dirty then "is-success" else "" Button.button buttonText flavor "" enabled (fun _ -> dispatch SaveUser) + + let generalTabView (model:Model) (dispatch:Msg->Unit) (readonly: bool)= let modal = Modal.showCreate @@ -61,7 +63,7 @@ let generalTabView (model:Model) (dispatch:Msg->Unit) (readonly: bool)= Fields.text "Company" model.scratchUser.company [Disabled readonly] (Company >> UpdateField >> dispatch) Fields.checkbox "Can Login?" "Unchecking this stops the user from logging into Spark and entering orders" model.scratchUser.isActive [Disabled readonly] (IsActive >> UpdateField >> dispatch) Fields.checkbox "Can Create?" "Checking this allows the user to create other users and accounts" model.scratchUser.canCreate [Disabled readonly] (CanCreate >> UpdateField >> dispatch) - (if State.isNewUser model.scratchUser then modal else span[][]) + (if (State.isNewUser model.scratchUser) then modal else span[][]) ] let accountNameLookup (accountsList:BTSAccount list) (id:string) =