user fullname
This commit is contained in:
		
							parent
							
								
									9170785784
								
							
						
					
					
						commit
						008c776d93
					
				
							
								
								
									
										118
									
								
								README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										118
									
								
								README.md
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,118 @@ | |||||||
|  | ### 认证相关路由 | ||||||
|  | 
 | ||||||
|  | 1. **注册用户** | ||||||
|  |    ```sh | ||||||
|  |    curl -X POST http://localhost:8080/auth/register \ | ||||||
|  |    -H "Content-Type: application/json" \ | ||||||
|  |    -d '{"username": "testuser", "password": "testpass", "email": "test@example.com"}' | ||||||
|  |    ``` | ||||||
|  | 
 | ||||||
|  | 2. **登录用户** | ||||||
|  |    ```sh | ||||||
|  |    curl -X POST http://localhost:8080/auth/login \ | ||||||
|  |    -H "Content-Type: application/json" \ | ||||||
|  |    -d '{"username": "testuser", "password": "testpass"}' | ||||||
|  |    ``` | ||||||
|  | 
 | ||||||
|  | ### 需要认证的路由 | ||||||
|  | 
 | ||||||
|  | 假设登录后返回的token为`your_jwt_token_here`。 | ||||||
|  | 
 | ||||||
|  | #### 用户管理 | ||||||
|  | 
 | ||||||
|  | 3. **获取所有用户** | ||||||
|  |    ```sh | ||||||
|  |    curl -X GET http://localhost:8080/api/users \ | ||||||
|  |    -H "Authorization: Bearer your_jwt_token_here" | ||||||
|  |    ``` | ||||||
|  | 
 | ||||||
|  | 4. **获取单个用户** | ||||||
|  |    ```sh | ||||||
|  |    curl -X GET http://localhost:8080/api/users/1 \ | ||||||
|  |    -H "Authorization: Bearer your_jwt_token_here" | ||||||
|  |    ``` | ||||||
|  | 
 | ||||||
|  | 5. **更新用户** | ||||||
|  |    ```sh | ||||||
|  |    curl -X PUT http://localhost:8080/api/users/1 \ | ||||||
|  |    -H "Content-Type: application/json" \ | ||||||
|  |    -H "Authorization: Bearer your_jwt_token_here" \ | ||||||
|  |    -d '{"name": "updated_name", "email": "updated_email@example.com"}' | ||||||
|  |    ``` | ||||||
|  | 
 | ||||||
|  | 6. **删除用户** | ||||||
|  |    ```sh | ||||||
|  |    curl -X DELETE http://localhost:8080/api/users/1 \ | ||||||
|  |    -H "Authorization: Bearer your_jwt_token_here" | ||||||
|  |    ``` | ||||||
|  | 
 | ||||||
|  | #### 角色管理 | ||||||
|  | 
 | ||||||
|  | 7. **创建新角色** | ||||||
|  |    ```sh | ||||||
|  |    curl -X POST http://localhost:8080/api/roles \ | ||||||
|  |    -H "Content-Type: application/json" \ | ||||||
|  |    -H "Authorization: Bearer your_jwt_token_here" \ | ||||||
|  |    -d '{"name": "admin", "description": "Administrator role"}' | ||||||
|  |    ``` | ||||||
|  | 
 | ||||||
|  | 8. **获取所有角色** | ||||||
|  |    ```sh | ||||||
|  |    curl -X GET http://localhost:8080/api/roles \ | ||||||
|  |    -H "Authorization: Bearer your_jwt_token_here" | ||||||
|  |    ``` | ||||||
|  | 
 | ||||||
|  | 9. **获取单个角色** | ||||||
|  |    ```sh | ||||||
|  |    curl -X GET http://localhost:8080/api/roles/1 \ | ||||||
|  |    -H "Authorization: Bearer your_jwt_token_here" | ||||||
|  |    ``` | ||||||
|  | 
 | ||||||
|  | 10. **更新角色** | ||||||
|  |     ```sh | ||||||
|  |     curl -X PUT http://localhost:8080/api/roles/1 \ | ||||||
|  |     -H "Content-Type: application/json" \ | ||||||
|  |     -H "Authorization: Bearer your_jwt_token_here" \ | ||||||
|  |     -d '{"name": "updated_role_name", "description": "Updated description"}' | ||||||
|  |     ``` | ||||||
|  | 
 | ||||||
|  | 11. **删除角色** | ||||||
|  |     ```sh | ||||||
|  |     curl -X DELETE http://localhost:8080/api/roles/1 \ | ||||||
|  |     -H "Authorization: Bearer your_jwt_token_here" | ||||||
|  |     ``` | ||||||
|  | 
 | ||||||
|  | #### 权限管理 | ||||||
|  | 
 | ||||||
|  | 12. **创建新权限** | ||||||
|  |     ```sh | ||||||
|  |     curl -X POST http://localhost:8080/api/permissions \ | ||||||
|  |     -H "Content-Type: application/json" \ | ||||||
|  |     -H "Authorization: Bearer your_jwt_token_here" \ | ||||||
|  |     -d '{"name": "create_user", "description": "Create user permission", "resource": "user", "action": "create"}' | ||||||
|  |     ``` | ||||||
|  | 
 | ||||||
|  | 13. **获取所有权限** | ||||||
|  |     ```sh | ||||||
|  |     curl -X GET http://localhost:8080/api/permissions \ | ||||||
|  |     -H "Authorization: Bearer your_jwt_token_here" | ||||||
|  |     ``` | ||||||
|  | 
 | ||||||
|  | 14. **获取单个权限** | ||||||
|  |     ```sh | ||||||
|  |     curl -X GET http://localhost:8080/api/permissions/1 \ | ||||||
|  |     -H "Authorization: Bearer your_jwt_token_here" | ||||||
|  |     ``` | ||||||
|  | 
 | ||||||
|  | 15. **更新权限** | ||||||
|  |     ```sh | ||||||
|  |     curl -X PUT http://localhost:8080/api/permissions/1 \ | ||||||
|  |     -H "Content-Type: application/json" \ | ||||||
|  |     -H "Authorization: Bearer your_jwt_token_here" \ | ||||||
|  |     -d '{"name": "updated_permission_name", "description": "Updated description", "resource": "user", "action": "update"}' | ||||||
|  |     ``` | ||||||
|  | 
 | ||||||
|  | 16. **删除权限** | ||||||
|  |     ```sh | ||||||
|  |     curl -X DELETE http://localhost:8080/api/permissions/1 \ | ||||||
|  |     -H "Authorization: Bearer your_jwt_token_here" | ||||||
| @ -8,6 +8,7 @@ import ( | |||||||
| func Register(c *gin.Context) { | func Register(c *gin.Context) { | ||||||
| 	var registerData struct { | 	var registerData struct { | ||||||
| 		Username string `json:"username"` | 		Username string `json:"username"` | ||||||
|  | 		Fullname string `json:"fullname"` | ||||||
| 		Password string `json:"password"` | 		Password string `json:"password"` | ||||||
| 		Email    string `json:"email"` | 		Email    string `json:"email"` | ||||||
| 	} | 	} | ||||||
| @ -15,7 +16,7 @@ func Register(c *gin.Context) { | |||||||
| 		c.JSON(400, gin.H{"error": err.Error()}) | 		c.JSON(400, gin.H{"error": err.Error()}) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	user, err := services.RegisterUser(registerData.Username, registerData.Password, registerData.Email) | 	user, err := services.RegisterUser(registerData.Username, registerData.Fullname, registerData.Password, registerData.Email) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		c.JSON(500, gin.H{"error": err.Error()}) | 		c.JSON(500, gin.H{"error": err.Error()}) | ||||||
| 		return | 		return | ||||||
|  | |||||||
| @ -1,9 +1,10 @@ | |||||||
| package controllers | package controllers | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
|  | 	"strconv" | ||||||
|  | 
 | ||||||
| 	"gitea.zjmud.xyz/phyer/rbac/services" | 	"gitea.zjmud.xyz/phyer/rbac/services" | ||||||
| 	"github.com/gin-gonic/gin" | 	"github.com/gin-gonic/gin" | ||||||
| 	"strconv" |  | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| // CreateRole 创建新角色 | // CreateRole 创建新角色 | ||||||
|  | |||||||
							
								
								
									
										5
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										5
									
								
								main.go
									
									
									
									
									
								
							| @ -3,12 +3,15 @@ package main | |||||||
| import ( | import ( | ||||||
| 	"gitea.zjmud.xyz/phyer/rbac/api" | 	"gitea.zjmud.xyz/phyer/rbac/api" | ||||||
| 	"gitea.zjmud.xyz/phyer/rbac/config" | 	"gitea.zjmud.xyz/phyer/rbac/config" | ||||||
|  | 	"gitea.zjmud.xyz/phyer/rbac/repositories" | ||||||
| 	"gitea.zjmud.xyz/phyer/rbac/server" | 	"gitea.zjmud.xyz/phyer/rbac/server" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func main() { | func main() { | ||||||
| 	config.Init() | 	config.Init() | ||||||
| 	server.InitDB() | 	if err := repositories.InitDB(); err != nil { | ||||||
|  | 		panic("failed to initialize database: " + err.Error()) | ||||||
|  | 	} | ||||||
| 	r := server.NewServer() | 	r := server.NewServer() | ||||||
| 	api.SetupRoutes(r) | 	api.SetupRoutes(r) | ||||||
| 	r.Run(":8080") | 	r.Run(":8080") | ||||||
|  | |||||||
| @ -1,13 +1,15 @@ | |||||||
| package models | package models | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"gorm.io/gorm" |  | ||||||
| 	"time" | 	"time" | ||||||
|  | 
 | ||||||
|  | 	"gorm.io/gorm" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| type User struct { | type User struct { | ||||||
| 	gorm.Model | 	gorm.Model | ||||||
| 	Username  string    `json:"username" gorm:"uniqueIndex;not null"` | 	Username  string    `json:"username" gorm:"uniqueIndex;not null"` | ||||||
|  | 	Fullname  string    `json:"fullname" gorm:"not null"` | ||||||
| 	Password  string    `json:"-" gorm:"not null"` | 	Password  string    `json:"-" gorm:"not null"` | ||||||
| 	Email     string    `json:"email" gorm:"uniqueIndex;not null"` | 	Email     string    `json:"email" gorm:"uniqueIndex;not null"` | ||||||
| 	CreatedAt time.Time `json:"created_at"` | 	CreatedAt time.Time `json:"created_at"` | ||||||
|  | |||||||
| @ -1,8 +1,9 @@ | |||||||
| package models | package models | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"gorm.io/gorm" |  | ||||||
| 	"time" | 	"time" | ||||||
|  | 
 | ||||||
|  | 	"gorm.io/gorm" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| // UserGroup 表示用户组模型 | // UserGroup 表示用户组模型 | ||||||
|  | |||||||
| @ -5,17 +5,17 @@ import ( | |||||||
| 	"gitea.zjmud.xyz/phyer/rbac/models" | 	"gitea.zjmud.xyz/phyer/rbac/models" | ||||||
| 	"gorm.io/driver/mysql" | 	"gorm.io/driver/mysql" | ||||||
| 	"gorm.io/gorm" | 	"gorm.io/gorm" | ||||||
| 	"strconv" | 	// "strconv" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| var db *gorm.DB | var db *gorm.DB | ||||||
| 
 | 
 | ||||||
| func InitDB() error { | func InitDB() error { | ||||||
| 	var err error | 	var err error | ||||||
| 	dsn := config.AppConfig.DB.User + ":" + config.AppConfig.DB.Password + "@tcp(" + config.AppConfig.DB.Host + ":" + strconv.Itoa(config.AppConfig.DB.Port) + ")/" + config.AppConfig.DB.Name + "?charset=utf8mb4&parseTime=True&loc=Local" | 	dsn := config.AppConfig.DB.DSN() | ||||||
| 	db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{}) | 	db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{}) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
| 	return db.AutoMigrate(&models.Permission{}, &models.Role{}, &models.UserGroup{}) | 	return db.AutoMigrate(&models.Permission{}, &models.Role{}, &models.UserGroup{}, &models.User{}) | ||||||
| } | } | ||||||
|  | |||||||
| @ -2,23 +2,13 @@ package server | |||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"gitea.zjmud.xyz/phyer/rbac/api" | 	"gitea.zjmud.xyz/phyer/rbac/api" | ||||||
| 	"gitea.zjmud.xyz/phyer/rbac/config" | 	// "gitea.zjmud.xyz/phyer/rbac/config" | ||||||
| 	"github.com/gin-gonic/gin" | 	"github.com/gin-gonic/gin" | ||||||
| 	"gorm.io/driver/mysql" | 	// "gorm.io/driver/mysql" | ||||||
| 	"gorm.io/gorm" | 	// "gorm.io/gorm" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| var db *gorm.DB | // InitDB is now handled in repositories package | ||||||
| 
 |  | ||||||
| // InitDB initializes the database connection |  | ||||||
| func InitDB() { |  | ||||||
| 	var err error |  | ||||||
| 	dsn := config.AppConfig.DB.DSN() |  | ||||||
| 	db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{}) |  | ||||||
| 	if err != nil { |  | ||||||
| 		panic("failed to connect database") |  | ||||||
| 	} |  | ||||||
| } |  | ||||||
| 
 | 
 | ||||||
| // NewServer creates and returns a new Gin server | // NewServer creates and returns a new Gin server | ||||||
| func NewServer() *gin.Engine { | func NewServer() *gin.Engine { | ||||||
|  | |||||||
| @ -3,10 +3,11 @@ package services | |||||||
| import ( | import ( | ||||||
| 	"errors" | 	"errors" | ||||||
| 	// "gitea.zjmud.xyz/phyer/rbac/models" | 	// "gitea.zjmud.xyz/phyer/rbac/models" | ||||||
|  | 	"strconv" | ||||||
|  | 
 | ||||||
| 	"gitea.zjmud.xyz/phyer/rbac/repositories" | 	"gitea.zjmud.xyz/phyer/rbac/repositories" | ||||||
| 	"gitea.zjmud.xyz/phyer/rbac/utils" | 	"gitea.zjmud.xyz/phyer/rbac/utils" | ||||||
| 	"golang.org/x/crypto/bcrypt" | 	"golang.org/x/crypto/bcrypt" | ||||||
| 	"strconv" |  | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func Login(username, password string) (string, error) { | func Login(username, password string) (string, error) { | ||||||
|  | |||||||
| @ -2,9 +2,10 @@ package services | |||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"errors" | 	"errors" | ||||||
|  | 	"strconv" | ||||||
|  | 
 | ||||||
| 	"gitea.zjmud.xyz/phyer/rbac/models" | 	"gitea.zjmud.xyz/phyer/rbac/models" | ||||||
| 	"gitea.zjmud.xyz/phyer/rbac/repositories" | 	"gitea.zjmud.xyz/phyer/rbac/repositories" | ||||||
| 	"strconv" |  | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| // AssignRoleToUser 为用户分配角色 | // AssignRoleToUser 为用户分配角色 | ||||||
|  | |||||||
| @ -3,6 +3,7 @@ package services | |||||||
| import ( | import ( | ||||||
| 	"gitea.zjmud.xyz/phyer/rbac/models" | 	"gitea.zjmud.xyz/phyer/rbac/models" | ||||||
| 	"gitea.zjmud.xyz/phyer/rbac/repositories" | 	"gitea.zjmud.xyz/phyer/rbac/repositories" | ||||||
|  | 
 | ||||||
| 	// "gitea.zjmud.xyz/phyer/rbac/utils" | 	// "gitea.zjmud.xyz/phyer/rbac/utils" | ||||||
| 	"golang.org/x/crypto/bcrypt" | 	"golang.org/x/crypto/bcrypt" | ||||||
| ) | ) | ||||||
| @ -30,13 +31,14 @@ func DeleteUser(id string) error { | |||||||
| 	return repositories.DeleteUser(id) | 	return repositories.DeleteUser(id) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func RegisterUser(username, password, email string) (*models.User, error) { | func RegisterUser(username, fullname, password, email string) (*models.User, error) { | ||||||
| 	hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) | 	hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return nil, err | 		return nil, err | ||||||
| 	} | 	} | ||||||
| 	user := &models.User{ | 	user := &models.User{ | ||||||
| 		Username: username, | 		Username: username, | ||||||
|  | 		Fullname: fullname, | ||||||
| 		Password: string(hashedPassword), | 		Password: string(hashedPassword), | ||||||
| 		Email:    email, | 		Email:    email, | ||||||
| 	} | 	} | ||||||
|  | |||||||
| @ -2,9 +2,10 @@ package utils | |||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"errors" | 	"errors" | ||||||
|  | 	"time" | ||||||
|  | 
 | ||||||
| 	"gitea.zjmud.xyz/phyer/rbac/config" | 	"gitea.zjmud.xyz/phyer/rbac/config" | ||||||
| 	"github.com/golang-jwt/jwt/v5" | 	"github.com/golang-jwt/jwt/v5" | ||||||
| 	"time" |  | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func GenerateJWT(userID string) (string, error) { | func GenerateJWT(userID string) (string, error) { | ||||||
|  | |||||||
| @ -3,9 +3,10 @@ package utils | |||||||
| import ( | import ( | ||||||
| 	"context" | 	"context" | ||||||
| 	"fmt" | 	"fmt" | ||||||
|  | 	"time" | ||||||
|  | 
 | ||||||
| 	"gitea.zjmud.xyz/phyer/rbac/config" | 	"gitea.zjmud.xyz/phyer/rbac/config" | ||||||
| 	"github.com/go-redis/redis/v8" | 	"github.com/go-redis/redis/v8" | ||||||
| 	"time" |  | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| var ( | var ( | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 zhangkun9038@dingtalk.com
						zhangkun9038@dingtalk.com