Add default profile functionality

Default profile can be selected in settings.
pull/20/head
Harvey Tindall 4 years ago
parent 6b25215768
commit 89e70f6f7a
No known key found for this signature in database
GPG Key ID: BBC65952848FB1A2

@ -472,7 +472,9 @@ func (app *appContext) SetProfile(gc *gin.Context) {
func (app *appContext) GetProfiles(gc *gin.Context) {
app.storage.loadProfiles()
app.debug.Println("Profiles requested")
out := map[string]map[string]interface{}{}
out := map[string]interface{}{
"default_profile": app.storage.defaultProfile,
}
for name, p := range app.storage.profiles {
out[name] = map[string]interface{}{
"admin": p.Admin,
@ -484,6 +486,27 @@ func (app *appContext) GetProfiles(gc *gin.Context) {
gc.JSON(200, out)
}
func (app *appContext) SetDefaultProfile(gc *gin.Context) {
req := map[string]string{}
gc.BindJSON(&req)
app.info.Printf("Setting default profile to \"%s\"", req["name"])
if _, ok := app.storage.profiles[req["name"]]; !ok {
app.err.Printf("Profile not found: \"%s\"", req["name"])
respond(500, "Profile not found", gc)
return
}
for name, profile := range app.storage.profiles {
if name == req["name"] {
profile.Admin = true
app.storage.profiles[name] = profile
} else {
profile.Admin = false
}
}
app.storage.defaultProfile = req["name"]
gc.JSON(200, map[string]bool{"success": true})
}
type newProfileReq struct {
Name string `json:"name"`
ID string `json:"id"`
@ -584,10 +607,17 @@ func (app *appContext) GetInvites(gc *gin.Context) {
invites = append(invites, invite)
}
profiles := make([]string, len(app.storage.profiles))
i := 0
for p := range app.storage.profiles {
profiles[i] = p
i++
if len(app.storage.profiles) != 0 {
profiles[0] = app.storage.defaultProfile
i := 1
if len(app.storage.profiles) > 1 {
for p := range app.storage.profiles {
if p != app.storage.defaultProfile {
profiles[i] = p
i++
}
}
}
}
resp := map[string]interface{}{
"profiles": profiles,

@ -608,7 +608,7 @@
"requires_restart": true,
"type": "text",
"value": "",
"description": "Location of stored user policy template (json)."
"description": "Deprecated. Location of stored user policy template (json)."
},
"user_configuration": {
"name": "userConfiguration",
@ -616,7 +616,7 @@
"requires_restart": true,
"type": "text",
"value": "",
"description": "Location of stored user configuration template (used for setting homescreen layout) (json)"
"description": "Deprecated. Location of stored user configuration template (used for setting homescreen layout) (json)"
},
"user_displayprefs": {
"name": "displayPreferences",
@ -624,7 +624,7 @@
"requires_restart": true,
"type": "text",
"value": "",
"description": "Location of stored displayPreferences template (also used for homescreen layout) (json)"
"description": "Deprecated. Location of stored displayPreferences template (also used for homescreen layout) (json)"
},
"user_profiles": {
"name": "User Profiles",
@ -632,7 +632,7 @@
"requires_restart": true,
"type": "text",
"value": "",
"description": "Location of stored user profiles (encompasses template and homescreen) (json)"
"description": "Location of stored user profiles (encompasses template and configuration and displayprefs) (json)"
},
"custom_css": {
"name": "Custom CSS",

@ -441,6 +441,7 @@
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Default</th>
<th scope="col">From</th>
<th scope="col">Admin?</th>
<th scope="col">Libraries</th>

@ -460,6 +460,7 @@ func start(asDaemon, firstCall bool) {
api.GET("/getInvites", app.GetInvites)
api.GET("/getProfiles", app.GetProfiles)
api.POST("/setProfile", app.SetProfile)
api.POST("/setDefaultProfile", app.SetDefaultProfile)
api.POST("/createProfile", app.CreateProfile)
api.POST("/deleteProfile", app.DeleteProfile)
api.POST("/setNotify", app.SetNotify)

@ -12,6 +12,7 @@ type Storage struct {
invite_path, emails_path, policy_path, configuration_path, displayprefs_path, ombi_path, profiles_path string
invites Invites
profiles map[string]Profile
defaultProfile string
emails, policy, configuration, displayprefs, ombi_template map[string]interface{}
}
@ -24,6 +25,7 @@ type Profile struct {
Policy map[string]interface{} `json:"policy,omitempty"`
Configuration map[string]interface{} `json:"configuration,omitempty"`
Displayprefs map[string]interface{} `json:"displayprefs,omitempty"`
Default bool `json:"default,omitempty"`
}
type Invite struct {
@ -90,6 +92,9 @@ func (st *Storage) storeOmbiTemplate() error {
func (st *Storage) loadProfiles() error {
err := loadJSON(st.profiles_path, &st.profiles)
for name, profile := range st.profiles {
if profile.Default {
st.defaultProfile = name
}
change := false
if profile.Policy["IsAdministrator"] != nil {
profile.Admin = profile.Policy["IsAdministrator"].(bool)
@ -112,8 +117,10 @@ func (st *Storage) loadProfiles() error {
st.profiles[name] = profile
}
}
if err != nil {
panic(err)
if st.defaultProfile == "" {
for n := range st.profiles {
st.defaultProfile = n
}
}
return err
}

@ -143,10 +143,12 @@ const populateProfiles = (noTable?: boolean): void => _get("/getProfiles", null,
if (this.readyState == 4 && this.status == 200) {
const profileList = document.getElementById('profileList');
profileList.textContent = '';
availableProfiles = [];
availableProfiles = [this.response["default_profile"]];
for (let name in this.response) {
availableProfiles.push(name);
if (!noTable) {
if (name != availableProfiles[0]) {
availableProfiles.push(name);
}
if (!noTable && name != "default_profile") {
const profile: Profile = {
Admin: this.response[name]["admin"],
LibraryAccess: this.response[name]["libraries"],
@ -154,16 +156,28 @@ const populateProfiles = (noTable?: boolean): void => _get("/getProfiles", null,
};
profileList.innerHTML += `
<td nowrap="nowrap" class="align-middle"><strong>${name}</strong></td>
<td nowrap="nowrap" class="align-middle"><input class="form-check-input" type="radio" name="defaultProfile" onclick="setDefaultProfile('${name}')" ${(name == availableProfiles[0]) ? "checked" : ""}></td>
<td nowrap="nowrap" class="align-middle">${profile.FromUser}</td>
<td nowrap="nowrap" class="align-middle">${profile.Admin ? "Yes" : "No"}</td>
<td nowrap="nowrap" class="align-middle">${profile.LibraryAccess}</td>
<td nowrap="nowrap" class="align-middle"><button class="btn btn-outline-danger" onclick="deleteProfile('${name}')">Delete</button></td>
<td nowrap="nowrap" class="align-middle"><button class="btn btn-outline-danger" id="defaultProfile_${name}" onclick="deleteProfile('${name}')">Delete</button></td>
`;
}
}
}
});
const setDefaultProfile = (name: string): void => _post("/setDefaultProfile", { "name": name }, function (): void {
if (this.readyState == 4) {
if (this.status != 200) {
(document.getElementById(`defaultProfile_${availableProfiles[0]}`) as HTMLInputElement).checked = true;
(document.getElementById(`defaultProfile_${name}`) as HTMLInputElement).checked = false;
} else {
generateInvites();
}
}
});
const deleteProfile = (name: string): void => _post("/deleteProfile", { "name": name }, function (): void {
if (this.readyState == 4 && this.status == 200) {
populateProfiles();
@ -224,10 +238,11 @@ function storeProfile(): void {
addAttr(button, "btn-primary");
rmAttr(button, "btn-success");
button.disabled = false;
populateProfiles();
userDefaultsModal.hide();
}, 1000);
populateProfiles();
generateInvites();
} else {
if ("error" in this.response) {
button.textContent = this.response["error"];

Loading…
Cancel
Save