| 
									
										
										
										
											2015-01-20 01:01:04 +08:00
										 |  |  | package api | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2015-02-05 17:37:13 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/bus" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/middleware" | 
					
						
							|  |  |  | 	m "github.com/grafana/grafana/pkg/models" | 
					
						
							| 
									
										
										
										
											2015-02-19 23:09:49 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/util" | 
					
						
							| 
									
										
										
										
											2015-01-20 01:01:04 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-18 23:28:15 +08:00
										 |  |  | // GET /api/user  (current authenticated user)
 | 
					
						
							|  |  |  | func GetSignedInUser(c *middleware.Context) Response { | 
					
						
							|  |  |  | 	return getUserUserProfile(c.UserId) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // GET /api/user/:id
 | 
					
						
							|  |  |  | func GetUserById(c *middleware.Context) Response { | 
					
						
							|  |  |  | 	return getUserUserProfile(c.ParamsInt64(":id")) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func getUserUserProfile(userId int64) Response { | 
					
						
							|  |  |  | 	query := m.GetUserProfileQuery{UserId: userId} | 
					
						
							| 
									
										
										
										
											2015-01-20 01:01:04 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if err := bus.Dispatch(&query); err != nil { | 
					
						
							| 
									
										
										
										
											2015-05-18 23:28:15 +08:00
										 |  |  | 		return ApiError(500, "Failed to get user", err) | 
					
						
							| 
									
										
										
										
											2015-01-20 01:01:04 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-18 23:28:15 +08:00
										 |  |  | 	return Json(200, query.Result) | 
					
						
							| 
									
										
										
										
											2015-01-20 01:01:04 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-19 01:06:19 +08:00
										 |  |  | // POST /api/user
 | 
					
						
							|  |  |  | func UpdateSignedInUser(c *middleware.Context, cmd m.UpdateUserCommand) Response { | 
					
						
							| 
									
										
										
										
											2015-01-20 01:01:04 +08:00
										 |  |  | 	cmd.UserId = c.UserId | 
					
						
							| 
									
										
										
										
											2015-05-19 01:06:19 +08:00
										 |  |  | 	return handleUpdateUser(cmd) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // POST /api/users/:id
 | 
					
						
							|  |  |  | func UpdateUser(c *middleware.Context, cmd m.UpdateUserCommand) Response { | 
					
						
							|  |  |  | 	cmd.UserId = c.ParamsInt64(":id") | 
					
						
							|  |  |  | 	return handleUpdateUser(cmd) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func handleUpdateUser(cmd m.UpdateUserCommand) Response { | 
					
						
							|  |  |  | 	if len(cmd.Login) == 0 { | 
					
						
							|  |  |  | 		cmd.Login = cmd.Email | 
					
						
							|  |  |  | 		if len(cmd.Login) == 0 { | 
					
						
							|  |  |  | 			return ApiError(400, "Validation error, need specify either username or email", nil) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-01-20 01:01:04 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if err := bus.Dispatch(&cmd); err != nil { | 
					
						
							| 
									
										
										
										
											2015-05-19 01:06:19 +08:00
										 |  |  | 		return ApiError(500, "failed to update user", err) | 
					
						
							| 
									
										
										
										
											2015-01-20 01:01:04 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-19 01:06:19 +08:00
										 |  |  | 	return ApiSuccess("User updated") | 
					
						
							| 
									
										
										
										
											2015-01-20 01:01:04 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-18 23:28:15 +08:00
										 |  |  | // GET /api/user/orgs
 | 
					
						
							|  |  |  | func GetSignedInUserOrgList(c *middleware.Context) Response { | 
					
						
							|  |  |  | 	return getUserOrgList(c.UserId) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-01-20 01:01:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-18 23:28:15 +08:00
										 |  |  | // GET /api/user/:id/orgs
 | 
					
						
							|  |  |  | func GetUserOrgList(c *middleware.Context) Response { | 
					
						
							|  |  |  | 	return getUserOrgList(c.ParamsInt64(":id")) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-01-20 01:01:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-18 23:28:15 +08:00
										 |  |  | func getUserOrgList(userId int64) Response { | 
					
						
							|  |  |  | 	query := m.GetUserOrgListQuery{UserId: userId} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err := bus.Dispatch(&query); err != nil { | 
					
						
							|  |  |  | 		return ApiError(500, "Faile to get user organziations", err) | 
					
						
							| 
									
										
										
										
											2015-01-20 01:01:04 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-18 23:28:15 +08:00
										 |  |  | 	return Json(200, query.Result) | 
					
						
							| 
									
										
										
										
											2015-01-20 01:01:04 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-24 03:07:49 +08:00
										 |  |  | func validateUsingOrg(userId int64, orgId int64) bool { | 
					
						
							|  |  |  | 	query := m.GetUserOrgListQuery{UserId: userId} | 
					
						
							| 
									
										
										
										
											2015-01-20 01:01:04 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if err := bus.Dispatch(&query); err != nil { | 
					
						
							|  |  |  | 		return false | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-24 03:07:49 +08:00
										 |  |  | 	// validate that the org id in the list
 | 
					
						
							| 
									
										
										
										
											2015-01-20 01:01:04 +08:00
										 |  |  | 	valid := false | 
					
						
							|  |  |  | 	for _, other := range query.Result { | 
					
						
							| 
									
										
										
										
											2015-02-24 03:07:49 +08:00
										 |  |  | 		if other.OrgId == orgId { | 
					
						
							| 
									
										
										
										
											2015-01-20 01:01:04 +08:00
										 |  |  | 			valid = true | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return valid | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-20 20:59:38 +08:00
										 |  |  | // POST /api/user/using/:id
 | 
					
						
							|  |  |  | func UserSetUsingOrg(c *middleware.Context) Response { | 
					
						
							| 
									
										
										
										
											2015-02-24 03:07:49 +08:00
										 |  |  | 	orgId := c.ParamsInt64(":id") | 
					
						
							| 
									
										
										
										
											2015-01-20 01:01:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-24 03:07:49 +08:00
										 |  |  | 	if !validateUsingOrg(c.UserId, orgId) { | 
					
						
							| 
									
										
										
										
											2015-05-20 20:59:38 +08:00
										 |  |  | 		return ApiError(401, "Not a valid organization", nil) | 
					
						
							| 
									
										
										
										
											2015-01-20 01:01:04 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-20 20:59:38 +08:00
										 |  |  | 	cmd := m.SetUsingOrgCommand{UserId: c.UserId, OrgId: orgId} | 
					
						
							| 
									
										
										
										
											2015-01-20 01:01:04 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if err := bus.Dispatch(&cmd); err != nil { | 
					
						
							| 
									
										
										
										
											2015-05-20 20:59:38 +08:00
										 |  |  | 		return ApiError(500, "Failed change active organization", err) | 
					
						
							| 
									
										
										
										
											2015-01-20 01:01:04 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-20 20:59:38 +08:00
										 |  |  | 	return ApiSuccess("Active organization changed") | 
					
						
							| 
									
										
										
										
											2015-01-20 01:01:04 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-02-19 23:09:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-20 20:59:38 +08:00
										 |  |  | func ChangeUserPassword(c *middleware.Context, cmd m.ChangeUserPasswordCommand) Response { | 
					
						
							| 
									
										
										
										
											2015-02-19 23:09:49 +08:00
										 |  |  | 	userQuery := m.GetUserByIdQuery{Id: c.UserId} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err := bus.Dispatch(&userQuery); err != nil { | 
					
						
							| 
									
										
										
										
											2015-05-20 20:59:38 +08:00
										 |  |  | 		return ApiError(500, "Could not read user from database", err) | 
					
						
							| 
									
										
										
										
											2015-02-19 23:09:49 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	passwordHashed := util.EncodePassword(cmd.OldPassword, userQuery.Result.Salt) | 
					
						
							|  |  |  | 	if passwordHashed != userQuery.Result.Password { | 
					
						
							| 
									
										
										
										
											2015-05-20 20:59:38 +08:00
										 |  |  | 		return ApiError(401, "Invalid old password", nil) | 
					
						
							| 
									
										
										
										
											2015-02-19 23:09:49 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if len(cmd.NewPassword) < 4 { | 
					
						
							| 
									
										
										
										
											2015-05-20 20:59:38 +08:00
										 |  |  | 		return ApiError(400, "New password too short", nil) | 
					
						
							| 
									
										
										
										
											2015-02-19 23:09:49 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	cmd.UserId = c.UserId | 
					
						
							|  |  |  | 	cmd.NewPassword = util.EncodePassword(cmd.NewPassword, userQuery.Result.Salt) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err := bus.Dispatch(&cmd); err != nil { | 
					
						
							| 
									
										
										
										
											2015-05-20 20:59:38 +08:00
										 |  |  | 		return ApiError(500, "Failed to change user password", err) | 
					
						
							| 
									
										
										
										
											2015-02-19 23:09:49 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-20 20:59:38 +08:00
										 |  |  | 	return ApiSuccess("User password changed") | 
					
						
							| 
									
										
										
										
											2015-02-19 23:09:49 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-05-19 17:47:14 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // GET /api/users
 | 
					
						
							|  |  |  | func SearchUsers(c *middleware.Context) Response { | 
					
						
							|  |  |  | 	query := m.SearchUsersQuery{Query: "", Page: 0, Limit: 1000} | 
					
						
							|  |  |  | 	if err := bus.Dispatch(&query); err != nil { | 
					
						
							|  |  |  | 		return ApiError(500, "Failed to fetch users", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return Json(200, query.Result) | 
					
						
							|  |  |  | } |