-
Notifications
You must be signed in to change notification settings - Fork 27
fix(licenses): enforce min one obligation requirement #191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -252,6 +252,19 @@ func CreateLicense(c *gin.Context) { | |
| return | ||
| } | ||
|
|
||
| // Validate that at least one obligation is provided | ||
| if input.Obligations == nil || len(*input.Obligations) == 0 { | ||
| er := models.LicenseError{ | ||
| Status: http.StatusBadRequest, | ||
| Message: "can not create license with these field values", | ||
| Error: "license must have at least one obligation", | ||
| Path: c.Request.URL.Path, | ||
| Timestamp: time.Now().Format(time.RFC3339), | ||
| } | ||
| c.JSON(http.StatusBadRequest, er) | ||
| return | ||
| } | ||
|
|
||
| lic := input.ConvertToLicenseDB() | ||
|
|
||
| lic.UserId = userId | ||
|
|
@@ -271,7 +284,7 @@ func CreateLicense(c *gin.Context) { | |
| return result.Error | ||
| } | ||
|
|
||
| if err := tx.Preload("User").First(&lic).Error; err != nil { | ||
| if err := tx.Preload("User").Preload("Obligations").First(&lic).Error; err != nil { | ||
| er := models.LicenseError{ | ||
| Status: http.StatusInternalServerError, | ||
| Message: "Failed to create license", | ||
|
|
@@ -283,6 +296,19 @@ func CreateLicense(c *gin.Context) { | |
| return result.Error | ||
| } | ||
|
|
||
| // Validate that license has at least one obligation after creation | ||
| if len(lic.Obligations) == 0 { | ||
| er := models.LicenseError{ | ||
| Status: http.StatusBadRequest, | ||
| Message: "can not create license with these field values", | ||
| Error: "license must have at least one obligation", | ||
| Path: c.Request.URL.Path, | ||
| Timestamp: time.Now().Format(time.RFC3339), | ||
| } | ||
| c.JSON(http.StatusBadRequest, er) | ||
| return errors.New("license must have at least one obligation") | ||
| } | ||
|
|
||
| if err := utils.AddChangelogsForLicense(tx, userId, &lic, &models.LicenseDB{}); err != nil { | ||
| er := models.LicenseError{ | ||
| Status: http.StatusInternalServerError, | ||
|
|
@@ -436,6 +462,19 @@ func UpdateLicense(c *gin.Context) { | |
| return err | ||
| } | ||
|
|
||
| // Validate that license has at least one obligation after update | ||
| if len(newLicense.Obligations) == 0 { | ||
| er := models.LicenseError{ | ||
| Status: http.StatusBadRequest, | ||
| Message: "can not update license with these field values", | ||
| Error: "license must have at least one obligation", | ||
| Path: c.Request.URL.Path, | ||
| Timestamp: time.Now().Format(time.RFC3339), | ||
| } | ||
| c.JSON(http.StatusBadRequest, er) | ||
| return errors.New("license must have at least one obligation") | ||
| } | ||
|
Comment on lines
+466
to
+476
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didnt see a step in UpdateLicense endpoint to updated or support obligation support too. Logically it should be there and when the user decides to update the License with Obligation we should put a check for that. Also this check doesnt make sense bcause: Line 453, we are already preLoading the obligations for the license, can we update the logic and check somewhere there? |
||
|
|
||
| if err := utils.AddChangelogsForLicense(tx, userId, &newLicense, &oldLicense); err != nil { | ||
| er := models.LicenseError{ | ||
| Status: http.StatusInternalServerError, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check here is kinda stale. Error message says: Cant create license with these field values. But it has already been created:
Also there are multiple checks after that too. So this looks like more of a defensive check. Can be removed.