Exemplo n.º 1
0
// Post creates a new user
func (u *User) Post(req *Request) {
	buff, err := ioutil.ReadAll(req.r.Body)
	if err != nil {
		http.Error(req.w, err.Error(), http.StatusBadRequest)
		logrus.Error(err)
		return
	}
	nur := &NewUserRequest{}
	err = json.Unmarshal(buff, nur)
	if err != nil {
		http.Error(req.w, err.Error(), http.StatusBadRequest)
		logrus.Error(err)
		return
	}

	err = u.pipeline.UpdateConfig(func(conf *config.AppConfig) error {

		// create the user in  the database
		nu := config.NewUser(nur.Name, nur.UserName, nur.Password, config.READ)
		err = conf.Provider().PutUser(nu)
		return nil

	}, req.u)

	if err != nil {
		http.Error(req.w, err.Error(), http.StatusInternalServerError)
		logrus.Error(err)
		return
	}
}
Exemplo n.º 2
0
func TestRemovePolicy(t *testing.T) {
	x := runningTestContext()
	x.runTest(func(p *Pipeline) {
		// make a test user
		u := config.NewUser("test", "test", "password", config.WRITE)

		// add an empty escalation policy
		p.UpdateConfig(func(c *config.AppConfig) error {
			c.Policies["hello"] = &escalation.Policy{}
			c.Policies["hello"].Compile(&testingPasser{})

			return nil
		}, u)

		// make sure the policy is there
		p.ViewConfig(func(c *config.AppConfig) {
			if _, ok := c.Policies["hello"]; !ok {
				t.Fatal("Policy was not added")
			}
		})

		// remove policy
		p.UpdateConfig(func(c *config.AppConfig) error {
			delete(c.Policies, "hello")
			return nil
		}, u)

		// make sure the policy is gone
		p.ViewConfig(func(c *config.AppConfig) {
			if _, ok := c.Policies["hello"]; ok {
				t.Fatal("Policy was not removed")
			}
		})
	})
}