2013-09-04 22:02:17 -04:00
|
|
|
/*
|
|
|
|
Copyright (C) 2013 Aaron Lindsay <aaron@aclindsay.com>
|
|
|
|
*/
|
|
|
|
|
2013-09-02 09:36:03 -04:00
|
|
|
package main
|
2013-08-23 00:09:03 -04:00
|
|
|
|
|
|
|
import (
|
2013-09-03 22:21:18 -04:00
|
|
|
"asink"
|
2013-08-23 00:09:03 -04:00
|
|
|
"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-09-02 23:35:48 -04:00
|
|
|
type ServerStopper int
|
|
|
|
|
|
|
|
func (s *ServerStopper) StopServer(code *int, result *int) error {
|
2013-09-03 22:21:18 -04:00
|
|
|
asink.Exit(*code)
|
2013-09-02 23:35:48 -04:00
|
|
|
*result = 0
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func StartRPC(sock string, tornDown chan int, adb *AsinkDB) {
|
2013-08-27 00:03:38 -04:00
|
|
|
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)
|
2013-09-02 23:35:48 -04:00
|
|
|
|
|
|
|
serverstop := new(ServerStopper)
|
|
|
|
rpc.Register(serverstop)
|
|
|
|
|
2013-08-23 00:09:03 -04:00
|
|
|
rpc.HandleHTTP()
|
2013-09-02 23:35:48 -04:00
|
|
|
l, err := net.Listen("unix", sock)
|
2013-08-23 00:09:03 -04:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer l.Close()
|
|
|
|
|
|
|
|
go http.Serve(l, nil)
|
|
|
|
|
2013-09-03 22:21:18 -04:00
|
|
|
asink.WaitOnExit()
|
2013-08-23 00:09:03 -04:00
|
|
|
}
|