forked from thomiceli/opengist
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request thomiceli#11 from thomiceli/feature/admin-panel-ch…
…anges Settings on admin panel
- Loading branch information
Showing
15 changed files
with
144 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package models | ||
|
||
import ( | ||
"errors" | ||
"github.com/mattn/go-sqlite3" | ||
"gorm.io/gorm/clause" | ||
) | ||
|
||
type AdminSetting struct { | ||
Key string `gorm:"uniqueIndex"` | ||
Value string | ||
} | ||
|
||
const ( | ||
SettingDisableSignup = "disable-signup" | ||
) | ||
|
||
func GetSetting(key string) (string, error) { | ||
var setting AdminSetting | ||
err := db.Where("key = ?", key).First(&setting).Error | ||
return setting.Value, err | ||
} | ||
|
||
func UpdateSetting(key string, value string) error { | ||
return db.Clauses(clause.OnConflict{ | ||
Columns: []clause.Column{{Name: "key"}}, // key colume | ||
DoUpdates: clause.AssignmentColumns([]string{"value"}), | ||
}).Create(&AdminSetting{ | ||
Key: key, | ||
Value: value, | ||
}).Error | ||
} | ||
|
||
func setSetting(key string, value string) error { | ||
return db.Create(&AdminSetting{Key: key, Value: value}).Error | ||
} | ||
|
||
func initAdminSettings(settings map[string]string) error { | ||
for key, value := range settings { | ||
if err := setSetting(key, value); err != nil { | ||
if !isUniqueConstraintViolation(err) { | ||
return err | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func isUniqueConstraintViolation(err error) bool { | ||
var sqliteErr sqlite3.Error | ||
if errors.As(err, &sqliteErr) && sqliteErr.ExtendedCode == sqlite3.ErrConstraintUnique { | ||
return true | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
document.addEventListener('DOMContentLoaded', () => { | ||
registerDomSetting(document.getElementById('disable-signup') as HTMLInputElement); | ||
}); | ||
|
||
const setSetting = (key: string, value: string) => { | ||
const data = new URLSearchParams(); | ||
data.append('key', key); | ||
data.append('value', value); | ||
data.append('_csrf', ((document.getElementsByName('_csrf')[0] as HTMLInputElement).value)); | ||
fetch('/admin-panel/set-setting', { | ||
method: 'PUT', | ||
credentials: 'same-origin', | ||
body: data, | ||
}); | ||
}; | ||
|
||
const registerDomSetting = (el: HTMLInputElement) => { | ||
el.addEventListener('change', () => { | ||
setSetting(el.id, el.checked ? '1' : '0'); | ||
}); | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters