2013-08-23 00:09:03 -04:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/rpc"
|
|
|
|
)
|
|
|
|
|
2013-08-27 00:03:38 -04:00
|
|
|
type UserModifier struct {
|
|
|
|
adb *AsinkDB
|
|
|
|
}
|
2013-08-23 00:09:03 -04:00
|
|
|
|
|
|
|
type UserModifierArgs struct {
|
|
|
|
Current *User
|
|
|
|
Updated *User
|
|
|
|
UpdateLogin bool
|
|
|
|
UpdateRole bool
|
|
|
|
UpdatePassword bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *UserModifier) AddUser(user *User, result *int) error {
|
2013-08-27 00:03:38 -04:00
|
|
|
err := u.adb.DatabaseAddUser(user)
|
|
|
|
if err != nil {
|
|
|
|
*result = 1
|
|
|
|
} else {
|
|
|
|
*result = 0
|
|
|
|
}
|
|
|
|
return err
|
2013-08-23 00:09:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *UserModifier) ModifyUser(args *UserModifierArgs, result *int) error {
|
2013-08-27 00:03:38 -04:00
|
|
|
currentUser, err := u.adb.DatabaseGetUser(args.Current.Username)
|
|
|
|
if err != nil {
|
|
|
|
*result = 1
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if args.UpdateLogin {
|
|
|
|
currentUser.Username = args.Updated.Username
|
|
|
|
}
|
|
|
|
if args.UpdateRole {
|
|
|
|
currentUser.Role = args.Updated.Role
|
|
|
|
}
|
|
|
|
if args.UpdatePassword {
|
|
|
|
currentUser.PWHash = args.Updated.PWHash
|
|
|
|
}
|
|
|
|
|
|
|
|
err = u.adb.DatabaseUpdateUser(currentUser)
|
|
|
|
if err != nil {
|
|
|
|
*result = 1
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*result = 0
|
2013-08-23 00:09:03 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *UserModifier) RemoveUser(user *User, result *int) error {
|
2013-08-27 00:03:38 -04:00
|
|
|
err := u.adb.DatabaseDeleteUser(user)
|
|
|
|
if err != nil {
|
|
|
|
*result = 1
|
|
|
|
} else {
|
|
|
|
*result = 0
|
|
|
|
}
|
|
|
|
return err
|
2013-08-23 00:09:03 -04:00
|
|
|
}
|
|
|
|
|
2013-08-27 00:03:38 -04:00
|
|
|
func StartRPC(tornDown chan int, adb *AsinkDB) {
|
|
|
|
defer func() { tornDown <- 0 }() //the main thread waits for this to ensure the socket is closed
|
2013-08-23 00:09:03 -04:00
|
|
|
|
|
|
|
usermod := new(UserModifier)
|
2013-08-27 00:03:38 -04:00
|
|
|
usermod.adb = adb
|
|
|
|
|
2013-08-23 00:09:03 -04:00
|
|
|
rpc.Register(usermod)
|
|
|
|
rpc.HandleHTTP()
|
|
|
|
l, err := net.Listen("unix", "/tmp/asink.sock")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer l.Close()
|
|
|
|
|
|
|
|
go http.Serve(l, nil)
|
|
|
|
|
|
|
|
WaitOnExit()
|
|
|
|
}
|