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) { | ||||
| 	var registerData struct { | ||||
| 		Username string `json:"username"` | ||||
| 		Fullname string `json:"fullname"` | ||||
| 		Password string `json:"password"` | ||||
| 		Email    string `json:"email"` | ||||
| 	} | ||||
| @ -15,7 +16,7 @@ func Register(c *gin.Context) { | ||||
| 		c.JSON(400, gin.H{"error": err.Error()}) | ||||
| 		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 { | ||||
| 		c.JSON(500, gin.H{"error": err.Error()}) | ||||
| 		return | ||||
|  | ||||
| @ -1,9 +1,10 @@ | ||||
| package controllers | ||||
| 
 | ||||
| import ( | ||||
| 	"strconv" | ||||
| 
 | ||||
| 	"gitea.zjmud.xyz/phyer/rbac/services" | ||||
| 	"github.com/gin-gonic/gin" | ||||
| 	"strconv" | ||||
| ) | ||||
| 
 | ||||
| // CreateRole 创建新角色 | ||||
|  | ||||
							
								
								
									
										5
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										5
									
								
								main.go
									
									
									
									
									
								
							| @ -3,12 +3,15 @@ package main | ||||
| import ( | ||||
| 	"gitea.zjmud.xyz/phyer/rbac/api" | ||||
| 	"gitea.zjmud.xyz/phyer/rbac/config" | ||||
| 	"gitea.zjmud.xyz/phyer/rbac/repositories" | ||||
| 	"gitea.zjmud.xyz/phyer/rbac/server" | ||||
| ) | ||||
| 
 | ||||
| func main() { | ||||
| 	config.Init() | ||||
| 	server.InitDB() | ||||
| 	if err := repositories.InitDB(); err != nil { | ||||
| 		panic("failed to initialize database: " + err.Error()) | ||||
| 	} | ||||
| 	r := server.NewServer() | ||||
| 	api.SetupRoutes(r) | ||||
| 	r.Run(":8080") | ||||
|  | ||||
| @ -1,13 +1,15 @@ | ||||
| package models | ||||
| 
 | ||||
| import ( | ||||
| 	"gorm.io/gorm" | ||||
| 	"time" | ||||
| 
 | ||||
| 	"gorm.io/gorm" | ||||
| ) | ||||
| 
 | ||||
| type User struct { | ||||
| 	gorm.Model | ||||
| 	Username  string    `json:"username" gorm:"uniqueIndex;not null"` | ||||
| 	Fullname  string    `json:"fullname" gorm:"not null"` | ||||
| 	Password  string    `json:"-" gorm:"not null"` | ||||
| 	Email     string    `json:"email" gorm:"uniqueIndex;not null"` | ||||
| 	CreatedAt time.Time `json:"created_at"` | ||||
|  | ||||
| @ -1,8 +1,9 @@ | ||||
| package models | ||||
| 
 | ||||
| import ( | ||||
| 	"gorm.io/gorm" | ||||
| 	"time" | ||||
| 
 | ||||
| 	"gorm.io/gorm" | ||||
| ) | ||||
| 
 | ||||
| // UserGroup 表示用户组模型 | ||||
|  | ||||
| @ -5,17 +5,17 @@ import ( | ||||
| 	"gitea.zjmud.xyz/phyer/rbac/models" | ||||
| 	"gorm.io/driver/mysql" | ||||
| 	"gorm.io/gorm" | ||||
| 	"strconv" | ||||
| 	// "strconv" | ||||
| ) | ||||
| 
 | ||||
| var db *gorm.DB | ||||
| 
 | ||||
| func InitDB() 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{}) | ||||
| 	if err != nil { | ||||
| 		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 ( | ||||
| 	"gitea.zjmud.xyz/phyer/rbac/api" | ||||
| 	"gitea.zjmud.xyz/phyer/rbac/config" | ||||
| 	// "gitea.zjmud.xyz/phyer/rbac/config" | ||||
| 	"github.com/gin-gonic/gin" | ||||
| 	"gorm.io/driver/mysql" | ||||
| 	"gorm.io/gorm" | ||||
| 	// "gorm.io/driver/mysql" | ||||
| 	// "gorm.io/gorm" | ||||
| ) | ||||
| 
 | ||||
| var db *gorm.DB | ||||
| 
 | ||||
| // 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") | ||||
| 	} | ||||
| } | ||||
| // InitDB is now handled in repositories package | ||||
| 
 | ||||
| // NewServer creates and returns a new Gin server | ||||
| func NewServer() *gin.Engine { | ||||
|  | ||||
| @ -3,10 +3,11 @@ package services | ||||
| import ( | ||||
| 	"errors" | ||||
| 	// "gitea.zjmud.xyz/phyer/rbac/models" | ||||
| 	"strconv" | ||||
| 
 | ||||
| 	"gitea.zjmud.xyz/phyer/rbac/repositories" | ||||
| 	"gitea.zjmud.xyz/phyer/rbac/utils" | ||||
| 	"golang.org/x/crypto/bcrypt" | ||||
| 	"strconv" | ||||
| ) | ||||
| 
 | ||||
| func Login(username, password string) (string, error) { | ||||
|  | ||||
| @ -2,9 +2,10 @@ package services | ||||
| 
 | ||||
| import ( | ||||
| 	"errors" | ||||
| 	"strconv" | ||||
| 
 | ||||
| 	"gitea.zjmud.xyz/phyer/rbac/models" | ||||
| 	"gitea.zjmud.xyz/phyer/rbac/repositories" | ||||
| 	"strconv" | ||||
| ) | ||||
| 
 | ||||
| // AssignRoleToUser 为用户分配角色 | ||||
|  | ||||
| @ -3,6 +3,7 @@ package services | ||||
| import ( | ||||
| 	"gitea.zjmud.xyz/phyer/rbac/models" | ||||
| 	"gitea.zjmud.xyz/phyer/rbac/repositories" | ||||
| 
 | ||||
| 	// "gitea.zjmud.xyz/phyer/rbac/utils" | ||||
| 	"golang.org/x/crypto/bcrypt" | ||||
| ) | ||||
| @ -30,13 +31,14 @@ func DeleteUser(id string) error { | ||||
| 	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) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	user := &models.User{ | ||||
| 		Username: username, | ||||
| 		Fullname: fullname, | ||||
| 		Password: string(hashedPassword), | ||||
| 		Email:    email, | ||||
| 	} | ||||
|  | ||||
| @ -2,9 +2,10 @@ package utils | ||||
| 
 | ||||
| import ( | ||||
| 	"errors" | ||||
| 	"time" | ||||
| 
 | ||||
| 	"gitea.zjmud.xyz/phyer/rbac/config" | ||||
| 	"github.com/golang-jwt/jwt/v5" | ||||
| 	"time" | ||||
| ) | ||||
| 
 | ||||
| func GenerateJWT(userID string) (string, error) { | ||||
|  | ||||
| @ -3,9 +3,10 @@ package utils | ||||
| import ( | ||||
| 	"context" | ||||
| 	"fmt" | ||||
| 	"time" | ||||
| 
 | ||||
| 	"gitea.zjmud.xyz/phyer/rbac/config" | ||||
| 	"github.com/go-redis/redis/v8" | ||||
| 	"time" | ||||
| ) | ||||
| 
 | ||||
| var ( | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 zhangkun9038@dingtalk.com
						zhangkun9038@dingtalk.com