feat: fix LDAP page crash (#1630)

This commit is contained in:
Yaodong Yu
2023-03-08 21:31:55 +08:00
committed by GitHub
parent 7c4a6fea02
commit a5dfe54a33
7 changed files with 32 additions and 31 deletions

View File

@ -246,19 +246,24 @@ func (c *ApiController) Signup() {
// @Success 200 {object} controllers.Response The Response object // @Success 200 {object} controllers.Response The Response object
// @router /logout [get,post] // @router /logout [get,post]
func (c *ApiController) Logout() { func (c *ApiController) Logout() {
user := c.GetSessionUsername()
// https://openid.net/specs/openid-connect-rpinitiated-1_0-final.html // https://openid.net/specs/openid-connect-rpinitiated-1_0-final.html
accessToken := c.Input().Get("id_token_hint") accessToken := c.Input().Get("id_token_hint")
redirectUri := c.Input().Get("post_logout_redirect_uri") redirectUri := c.Input().Get("post_logout_redirect_uri")
state := c.Input().Get("state") state := c.Input().Get("state")
if accessToken == "" && redirectUri == "" { user := c.GetSessionUsername()
c.ClearUserSession()
// TODO https://github.com/casdoor/casdoor/pull/1494#discussion_r1095675265
owner, username := util.GetOwnerAndNameFromId(user)
if accessToken == "" && redirectUri == "" {
// TODO https://github.com/casdoor/casdoor/pull/1494#discussion_r1095675265
if user == "" {
c.ResponseOk()
return
}
c.ClearUserSession()
owner, username := util.GetOwnerAndNameFromId(user)
object.DeleteSessionId(util.GetSessionId(owner, username, object.CasdoorApplication), c.Ctx.Input.CruSession.SessionID()) object.DeleteSessionId(util.GetSessionId(owner, username, object.CasdoorApplication), c.Ctx.Input.CruSession.SessionID())
util.LogInfo(c.Ctx, "API: [%s] logged out", user) util.LogInfo(c.Ctx, "API: [%s] logged out", user)
application := c.GetSessionApplication() application := c.GetSessionApplication()

View File

@ -97,8 +97,7 @@ func (c *ApiController) GetLdapUser() {
}) })
} }
c.Data["json"] = Response{Status: "ok", Data: resp} c.ResponseOk(resp)
c.ServeJSON()
} }
// GetLdaps // GetLdaps
@ -108,8 +107,7 @@ func (c *ApiController) GetLdapUser() {
func (c *ApiController) GetLdaps() { func (c *ApiController) GetLdaps() {
owner := c.Input().Get("owner") owner := c.Input().Get("owner")
c.Data["json"] = Response{Status: "ok", Data: object.GetLdaps(owner)} c.ResponseOk(object.GetLdaps(owner))
c.ServeJSON()
} }
// GetLdap // GetLdap
@ -124,8 +122,8 @@ func (c *ApiController) GetLdap() {
return return
} }
c.Data["json"] = Response{Status: "ok", Data: object.GetLdap(id)} _, name := util.GetOwnerAndNameFromId(id)
c.ServeJSON() c.ResponseOk(object.GetLdap(name))
} }
// AddLdap // AddLdap
@ -159,8 +157,7 @@ func (c *ApiController) AddLdap() {
object.GetLdapAutoSynchronizer().StartAutoSync(ldap.Id) object.GetLdapAutoSynchronizer().StartAutoSync(ldap.Id)
} }
c.Data["json"] = resp c.ResponseOk(resp)
c.ServeJSON()
} }
// UpdateLdap // UpdateLdap
@ -187,8 +184,7 @@ func (c *ApiController) UpdateLdap() {
object.GetLdapAutoSynchronizer().StopAutoSync(ldap.Id) object.GetLdapAutoSynchronizer().StopAutoSync(ldap.Id)
} }
c.Data["json"] = resp c.ResponseOk(resp)
c.ServeJSON()
} }
// DeleteLdap // DeleteLdap
@ -204,8 +200,7 @@ func (c *ApiController) DeleteLdap() {
} }
object.GetLdapAutoSynchronizer().StopAutoSync(ldap.Id) object.GetLdapAutoSynchronizer().StopAutoSync(ldap.Id)
c.Data["json"] = wrapActionResponse(object.DeleteLdap(&ldap)) c.ResponseOk(wrapActionResponse(object.DeleteLdap(&ldap)))
c.ServeJSON()
} }
// SyncLdapUsers // SyncLdapUsers
@ -225,11 +220,11 @@ func (c *ApiController) SyncLdapUsers() {
object.UpdateLdapSyncTime(ldapId) object.UpdateLdapSyncTime(ldapId)
exist, failed := object.SyncLdapUsers(owner, users, ldapId) exist, failed := object.SyncLdapUsers(owner, users, ldapId)
c.Data["json"] = &Response{Status: "ok", Data: &LdapSyncResp{
c.ResponseOk(&LdapSyncResp{
Exist: *exist, Exist: *exist,
Failed: *failed, Failed: *failed,
}} })
c.ServeJSON()
} }
// CheckLdapUsersExist // CheckLdapUsersExist
@ -246,6 +241,5 @@ func (c *ApiController) CheckLdapUsersExist() {
} }
exist := object.CheckLdapUuidExist(owner, uuids) exist := object.CheckLdapUuidExist(owner, uuids)
c.Data["json"] = &Response{Status: "ok", Data: exist} c.ResponseOk(exist)
c.ServeJSON()
} }

View File

@ -516,8 +516,8 @@ class App extends Component {
<Route exact path="/applications/:organizationName/:applicationName" render={(props) => this.renderLoginIfNotLoggedIn(<ApplicationEditPage account={this.state.account} {...props} />)} /> <Route exact path="/applications/:organizationName/:applicationName" render={(props) => this.renderLoginIfNotLoggedIn(<ApplicationEditPage account={this.state.account} {...props} />)} />
<Route exact path="/resources" render={(props) => this.renderLoginIfNotLoggedIn(<ResourceListPage account={this.state.account} {...props} />)} /> <Route exact path="/resources" render={(props) => this.renderLoginIfNotLoggedIn(<ResourceListPage account={this.state.account} {...props} />)} />
{/* <Route exact path="/resources/:resourceName" render={(props) => this.renderLoginIfNotLoggedIn(<ResourceEditPage account={this.state.account} {...props} />)}/>*/} {/* <Route exact path="/resources/:resourceName" render={(props) => this.renderLoginIfNotLoggedIn(<ResourceEditPage account={this.state.account} {...props} />)}/>*/}
<Route exact path="/ldap/:ldapId" render={(props) => this.renderLoginIfNotLoggedIn(<LdapEditPage account={this.state.account} {...props} />)} /> <Route exact path="/ldap/:organizationName/:ldapId" render={(props) => this.renderLoginIfNotLoggedIn(<LdapEditPage account={this.state.account} {...props} />)} />
<Route exact path="/ldap/sync/:ldapId" render={(props) => this.renderLoginIfNotLoggedIn(<LdapSyncPage account={this.state.account} {...props} />)} /> <Route exact path="/ldap/sync/:organizationName/:ldapId" render={(props) => this.renderLoginIfNotLoggedIn(<LdapSyncPage account={this.state.account} {...props} />)} />
<Route exact path="/tokens" render={(props) => this.renderLoginIfNotLoggedIn(<TokenListPage account={this.state.account} {...props} />)} /> <Route exact path="/tokens" render={(props) => this.renderLoginIfNotLoggedIn(<TokenListPage account={this.state.account} {...props} />)} />
<Route exact path="/sessions" render={(props) => this.renderLoginIfNotLoggedIn(<SessionListPage account={this.state.account} {...props} />)} /> <Route exact path="/sessions" render={(props) => this.renderLoginIfNotLoggedIn(<SessionListPage account={this.state.account} {...props} />)} />
<Route exact path="/tokens/:tokenName" render={(props) => this.renderLoginIfNotLoggedIn(<TokenEditPage account={this.state.account} {...props} />)} /> <Route exact path="/tokens/:tokenName" render={(props) => this.renderLoginIfNotLoggedIn(<TokenEditPage account={this.state.account} {...props} />)} />

View File

@ -27,6 +27,7 @@ class LdapEditPage extends React.Component {
super(props); super(props);
this.state = { this.state = {
ldapId: props.match.params.ldapId, ldapId: props.match.params.ldapId,
organizationName: props.match.params.organizationName,
ldap: null, ldap: null,
organizations: [], organizations: [],
}; };
@ -38,7 +39,7 @@ class LdapEditPage extends React.Component {
} }
getLdap() { getLdap() {
LddpBackend.getLdap(this.state.ldapId) LddpBackend.getLdap(this.state.organizationName, this.state.ldapId)
.then((res) => { .then((res) => {
if (res.status === "ok") { if (res.status === "ok") {
this.setState({ this.setState({

View File

@ -23,6 +23,7 @@ class LdapSyncPage extends React.Component {
super(props); super(props);
this.state = { this.state = {
ldapId: props.match.params.ldapId, ldapId: props.match.params.ldapId,
organizationName: props.match.params.organizationName,
ldap: null, ldap: null,
users: [], users: [],
existUuids: [], existUuids: [],
@ -73,7 +74,7 @@ class LdapSyncPage extends React.Component {
} }
getLdap() { getLdap() {
LdapBackend.getLdap(this.state.ldapId) LdapBackend.getLdap(this.state.organizationName, this.state.ldapId)
.then((res) => { .then((res) => {
if (res.status === "ok") { if (res.status === "ok") {
this.setState((prevState) => { this.setState((prevState) => {

View File

@ -154,9 +154,9 @@ class LdapTable extends React.Component {
<div> <div>
<Button style={{marginTop: "10px", marginBottom: "10px", marginRight: "10px"}} <Button style={{marginTop: "10px", marginBottom: "10px", marginRight: "10px"}}
type="primary" type="primary"
onClick={() => Setting.goToLink(`/ldap/sync/${record.id}`)}>{i18next.t("ldap:Sync")}</Button> onClick={() => Setting.goToLink(`/ldap/sync/${record.owner}/${record.id}`)}>{i18next.t("ldap:Sync")}</Button>
<Button style={{marginTop: "10px", marginBottom: "10px", marginRight: "10px"}} <Button style={{marginTop: "10px", marginBottom: "10px", marginRight: "10px"}}
onClick={() => Setting.goToLink(`/ldap/${record.id}`)}>{i18next.t("general:Edit")}</Button> onClick={() => Setting.goToLink(`/ldap/${record.owner}/${record.id}`)}>{i18next.t("general:Edit")}</Button>
<Popconfirm <Popconfirm
title={i18next.t("general:Sure to delete") + `: ${record.serverName} ?`} title={i18next.t("general:Sure to delete") + `: ${record.serverName} ?`}
onConfirm={() => this.deleteRow(table, index)} onConfirm={() => this.deleteRow(table, index)}

View File

@ -24,8 +24,8 @@ export function getLdaps(owner) {
}).then(res => res.json()); }).then(res => res.json());
} }
export function getLdap(id) { export function getLdap(owner, name) {
return fetch(`${Setting.ServerUrl}/api/get-ldap?id=${id}`, { return fetch(`${Setting.ServerUrl}/api/get-ldap?id=${owner}/${encodeURIComponent(name)}`, {
method: "GET", method: "GET",
credentials: "include", credentials: "include",
headers: { headers: {