コード例 #1
0
ファイル: users.go プロジェクト: baiyuxiong/lms
func EditSelf(id int, password string, phone, phone1, email string) error {
	now := time.Now()

	if len(password) > 0 {
		u := new(models.Users)
		u.Salt = utils.Salt()
		u.UpdatedAt = now
		u.Password = utils.EncryptPassword(u.Salt, password)

		_, err := app.Engine.Id(id).Update(u)
		if err != nil {
			return errors.New("编辑用户失败" + err.Error())
		}
	}

	up := new(models.UserProfiles)
	up.Phone = phone
	up.Phone1 = phone1
	up.Email = email
	up.UpdatedAt = now

	_, err := app.Engine.Where("user_id=?", id).Update(up)
	if err != nil {
		return errors.New("编辑用户失败" + err.Error())
	}
	return nil
}
コード例 #2
0
ファイル: auth.go プロジェクト: baiyuxiong/lms
func (c Auth) DoLogin(username, password string) revel.Result {
	revel.INFO.Println("DoLogin")

	c.Validation.Required(username).Message("用户名不能为空")
	c.Validation.Required(password).Message("密码不能为空")

	if c.Validation.HasErrors() {
		c.Flash.Error(utils.ValidationErrorToString(c.Validation.Errors))
		c.Validation.Keep()
		c.FlashParams()
		revel.INFO.Println("DoLogin Validation error")
		return c.Redirect("/auth/login")
	} else {
		errorMsg := ""

		var u = &models.Users{Username: username}
		has, _ := app.Engine.Get(u)

		if !has {
			errorMsg = "用户名或密码出错"
		}

		if utils.EncryptPassword(u.Salt, password) != u.Password {
			errorMsg = "用户名或密码出错"
		}

		if len(errorMsg) > 0 {
			c.Flash.Error(errorMsg)
			revel.INFO.Println("DoLogin Validation error - ", errorMsg)
			c.Validation.Keep()
			c.FlashParams()
			return c.Redirect("/auth/login")
		}

		c.Session[utils.SESSION_KEY_UID] = fmt.Sprintf("%d", u.Id)

		u.UpdatedAt = time.Now()
		app.Engine.Id(u.Id).Cols("updated_at").Update(u)

		if username == utils.ADMIN_USERNAME {
			return c.Redirect("/admin")
		} else {
			return c.Redirect("/")
		}
	}
}
コード例 #3
0
ファイル: users.go プロジェクト: baiyuxiong/lms
func AddUser(username, password string, department_id, employment_type, gender, level int, name, phone, phone1, position, email string, join_date, birth_date time.Time, ip_address string) error {
	var u = &models.Users{Username: username}
	has, _ := app.Engine.Get(u)

	if has {
		return errors.New("用户名已被注册,不可用")
	}

	now := time.Now()
	u.IpAddress = strings.Split(ip_address, ":")[0]
	u.Salt = utils.Salt()
	u.Password = utils.EncryptPassword(u.Salt, password)
	u.IsActivited = 1
	u.ActivatedAt = now
	u.CreatedAt = now
	u.UpdatedAt = now

	_, err := app.Engine.Insert(u)
	if err != nil {
		return errors.New("添加用户失败" + err.Error())
	}

	profile := new(models.UserProfiles)
	profile.UserId = u.Id
	profile.DepartmentId = department_id
	profile.Gender = gender
	profile.Level = level
	profile.EmploymentType = employment_type
	profile.Name = name
	profile.Phone = phone
	profile.Phone1 = phone1
	profile.Position = position
	profile.JoinDate = join_date
	profile.Email = email
	profile.BirthDate = birth_date
	profile.CreatedAt = now
	profile.UpdatedAt = now
	_, err = app.Engine.Insert(profile)
	if err != nil {
		app.Engine.Id(u.Id).Delete(u)
		errors.New("添加用户失败" + err.Error())
	}
	return nil
}
コード例 #4
0
ファイル: users.go プロジェクト: baiyuxiong/lms
func EditUser(id int, username, password string, department_id, employment_type, gender, level int, name, phone, phone1, position, email string, join_date, birth_date time.Time, ip_address string) error {
	now := time.Now()

	u := new(models.Users)
	u.Username = username
	u.UpdatedAt = now
	u.IpAddress = ip_address

	if len(password) > 0 {
		u.Salt = utils.Salt()
		u.Password = utils.EncryptPassword(u.Salt, password)
	}

	_, err := app.Engine.Id(id).Update(u)
	if err != nil {
		return errors.New("编辑用户失败" + err.Error())
	}

	up := new(models.UserProfiles)
	up.DepartmentId = department_id
	up.Gender = gender
	up.Level = level
	up.EmploymentType = employment_type
	up.Name = name
	up.Phone = phone
	up.Phone1 = phone1
	up.Position = position
	up.JoinDate = join_date
	up.Email = email
	up.BirthDate = birth_date
	up.UpdatedAt = now

	_, err = app.Engine.Where("user_id=?", id).Update(up)
	if err != nil {
		return errors.New("编辑用户失败" + err.Error())
	}
	return nil
}