mirror of
https://github.com/casdoor/casdoor.git
synced 2025-07-09 09:23:40 +08:00
Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
7b0b426a76 | |||
a383af0ebc |
@ -119,12 +119,7 @@ func (c *ApiController) GetUser() {
|
|||||||
user = object.GetUser(id)
|
user = object.GetUser(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
if user != nil {
|
object.ExtendUserWithRolesAndPermissions(user)
|
||||||
roles := object.GetRolesByUser(user.GetId())
|
|
||||||
user.Roles = roles
|
|
||||||
permissions := object.GetPermissionsByUser(user.GetId())
|
|
||||||
user.Permissions = permissions
|
|
||||||
}
|
|
||||||
|
|
||||||
c.Data["json"] = object.GetMaskedUser(user)
|
c.Data["json"] = object.GetMaskedUser(user)
|
||||||
c.ServeJSON()
|
c.ServeJSON()
|
||||||
|
@ -27,6 +27,7 @@ import (
|
|||||||
func InitDb() {
|
func InitDb() {
|
||||||
existed := initBuiltInOrganization()
|
existed := initBuiltInOrganization()
|
||||||
if !existed {
|
if !existed {
|
||||||
|
initBuiltInModel()
|
||||||
initBuiltInPermission()
|
initBuiltInPermission()
|
||||||
initBuiltInProvider()
|
initBuiltInProvider()
|
||||||
initBuiltInUser()
|
initBuiltInUser()
|
||||||
@ -239,6 +240,33 @@ func initWebAuthn() {
|
|||||||
gob.Register(webauthn.SessionData{})
|
gob.Register(webauthn.SessionData{})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func initBuiltInModel() {
|
||||||
|
model := GetModel("built-in/model-built-in")
|
||||||
|
if model != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
model = &Model{
|
||||||
|
Owner: "built-in",
|
||||||
|
Name: "model-built-in",
|
||||||
|
CreatedTime: util.GetCurrentTime(),
|
||||||
|
DisplayName: "Built-in Model",
|
||||||
|
IsEnabled: true,
|
||||||
|
ModelText: `[request_definition]
|
||||||
|
r = sub, obj, act
|
||||||
|
|
||||||
|
[policy_definition]
|
||||||
|
p = sub, obj, act
|
||||||
|
|
||||||
|
[policy_effect]
|
||||||
|
e = some(where (p.eft == allow))
|
||||||
|
|
||||||
|
[matchers]
|
||||||
|
m = r.sub == p.sub && r.obj == p.obj && r.act == p.act`,
|
||||||
|
}
|
||||||
|
AddModel(model)
|
||||||
|
}
|
||||||
|
|
||||||
func initBuiltInPermission() {
|
func initBuiltInPermission() {
|
||||||
permission := GetPermission("built-in/permission-built-in")
|
permission := GetPermission("built-in/permission-built-in")
|
||||||
if permission != nil {
|
if permission != nil {
|
||||||
@ -253,6 +281,7 @@ func initBuiltInPermission() {
|
|||||||
Users: []string{"built-in/admin"},
|
Users: []string{"built-in/admin"},
|
||||||
Roles: []string{},
|
Roles: []string{},
|
||||||
Domains: []string{},
|
Domains: []string{},
|
||||||
|
Model: "model-built-in",
|
||||||
ResourceType: "Application",
|
ResourceType: "Application",
|
||||||
Resources: []string{"app-built-in"},
|
Resources: []string{"app-built-in"},
|
||||||
Actions: []string{"Read", "Write", "Admin"},
|
Actions: []string{"Read", "Write", "Admin"},
|
||||||
|
@ -17,6 +17,7 @@ package object
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/casbin/casbin/v2/model"
|
||||||
"github.com/casdoor/casdoor/util"
|
"github.com/casdoor/casdoor/util"
|
||||||
"xorm.io/core"
|
"xorm.io/core"
|
||||||
)
|
)
|
||||||
@ -85,13 +86,19 @@ func GetModel(id string) *Model {
|
|||||||
return getModel(owner, name)
|
return getModel(owner, name)
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateModel(id string, model *Model) bool {
|
func UpdateModel(id string, modelObj *Model) bool {
|
||||||
owner, name := util.GetOwnerAndNameFromId(id)
|
owner, name := util.GetOwnerAndNameFromId(id)
|
||||||
if getModel(owner, name) == nil {
|
if getModel(owner, name) == nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
affected, err := adapter.Engine.ID(core.PK{owner, name}).AllCols().Update(model)
|
// check model grammar
|
||||||
|
_, err := model.NewModelFromString(modelObj.ModelText)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
affected, err := adapter.Engine.ID(core.PK{owner, name}).AllCols().Update(modelObj)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
@ -287,6 +287,7 @@ func GetOAuthCode(userId string, clientId string, responseType string, redirectU
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ExtendUserWithRolesAndPermissions(user)
|
||||||
accessToken, refreshToken, tokenName, err := generateJwtToken(application, user, nonce, scope, host)
|
accessToken, refreshToken, tokenName, err := generateJwtToken(application, user, nonce, scope, host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
@ -421,6 +422,7 @@ func RefreshToken(grantType string, refreshToken string, scope string, clientId
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ExtendUserWithRolesAndPermissions(user)
|
||||||
newAccessToken, newRefreshToken, tokenName, err := generateJwtToken(application, user, "", scope, host)
|
newAccessToken, newRefreshToken, tokenName, err := generateJwtToken(application, user, "", scope, host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &TokenError{
|
return &TokenError{
|
||||||
@ -571,6 +573,7 @@ func GetPasswordToken(application *Application, username string, password string
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ExtendUserWithRolesAndPermissions(user)
|
||||||
accessToken, refreshToken, tokenName, err := generateJwtToken(application, user, "", scope, host)
|
accessToken, refreshToken, tokenName, err := generateJwtToken(application, user, "", scope, host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, &TokenError{
|
return nil, &TokenError{
|
||||||
@ -640,6 +643,7 @@ func GetClientCredentialsToken(application *Application, clientSecret string, sc
|
|||||||
// GetTokenByUser
|
// GetTokenByUser
|
||||||
// Implicit flow
|
// Implicit flow
|
||||||
func GetTokenByUser(application *Application, user *User, scope string, host string) (*Token, error) {
|
func GetTokenByUser(application *Application, user *User, scope string, host string) (*Token, error) {
|
||||||
|
ExtendUserWithRolesAndPermissions(user)
|
||||||
accessToken, refreshToken, tokenName, err := generateJwtToken(application, user, "", scope, host)
|
accessToken, refreshToken, tokenName, err := generateJwtToken(application, user, "", scope, host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -726,6 +730,7 @@ func GetWechatMiniProgramToken(application *Application, code string, host strin
|
|||||||
AddUser(user)
|
AddUser(user)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ExtendUserWithRolesAndPermissions(user)
|
||||||
accessToken, refreshToken, tokenName, err := generateJwtToken(application, user, "", "", host)
|
accessToken, refreshToken, tokenName, err := generateJwtToken(application, user, "", "", host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, &TokenError{
|
return nil, &TokenError{
|
||||||
|
@ -566,3 +566,12 @@ func (user *User) GetId() string {
|
|||||||
func isUserIdGlobalAdmin(userId string) bool {
|
func isUserIdGlobalAdmin(userId string) bool {
|
||||||
return strings.HasPrefix(userId, "built-in/")
|
return strings.HasPrefix(userId, "built-in/")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExtendUserWithRolesAndPermissions(user *User) {
|
||||||
|
if user == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
user.Roles = GetRolesByUser(user.GetId())
|
||||||
|
user.Permissions = GetPermissionsByUser(user.GetId())
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user