mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-28 13:48:49 +08:00

* adding group Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * adding user group Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * adding group Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add groups Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add groups Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add retrieve methods Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add default admin user Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add default admin user Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * adding endpoints Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * adding endpoints Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * adding tests Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * changes signature for AssignUser Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * adding tests Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * bug fixing retrieving groups Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove unused code Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * bug fixing retrieving groups Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * retrieve groups Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * change environment for admin Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * change environment for admin Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * retrieve groups Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove adding default group Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * expose port for debugging purposes Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix tests, and linter errors Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add prefix Users for groups endpoint Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix linter problems Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix endpoint prefix url Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix endpoint test Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add group features in cli Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove comments Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove println Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * when user is created return id in response Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * when user is created return id in response Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * adding default admin env Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * proper alignment Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * proper alignment Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix comments Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * rename method Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * return user id when created Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * return user id when created Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove unused variable Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * rename methods Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix to retrieve whole tree starting from parent Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add endpoint to list groups for user Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add readme for groups Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fixing bugs Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fixing bugs Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add group commands for add and remove user Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * replace default email, use example.com Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix capital letters beginning of sentence Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove warning for deprecated api, mistakenly copied Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * simplify repo methods, rely on db driver rather than the check before operation Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * check if group is valid Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * openapi spec 3.0 Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove check for existing users in groups before delete Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * renaming methods Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * renaming methods Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * renaming methods Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * change func signature Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * change func signature Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix bugs, resolve comments Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix bugs, resolve comments Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix alignment Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add missing command Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * reorganize envs Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix doc Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix compile Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * reorganize cli commands Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * minor corrections Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * renaming Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * renaming Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * renaming Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * rename methods Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix naming Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * renaming Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * renaming Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * resolve comments, minor changes Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com>
336 lines
8.9 KiB
Go
336 lines
8.9 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package api
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/go-kit/kit/endpoint"
|
|
"github.com/mainflux/mainflux/users"
|
|
)
|
|
|
|
func registrationEndpoint(svc users.Service) endpoint.Endpoint {
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(userReq)
|
|
if err := req.validate(); err != nil {
|
|
return createUserRes{}, err
|
|
}
|
|
uid, err := svc.Register(ctx, req.user)
|
|
if err != nil {
|
|
return createUserRes{}, err
|
|
}
|
|
ucr := createUserRes{
|
|
ID: uid,
|
|
created: true,
|
|
}
|
|
logger.Info("User successfully registered")
|
|
return ucr, nil
|
|
}
|
|
}
|
|
|
|
// Password reset request endpoint.
|
|
// When successful password reset link is generated.
|
|
// Link is generated using MF_TOKEN_RESET_ENDPOINT env.
|
|
// and value from Referer header for host.
|
|
// {Referer}+{MF_TOKEN_RESET_ENDPOINT}+{token=TOKEN}
|
|
// http://mainflux.com/reset-request?token=xxxxxxxxxxx.
|
|
// Email with a link is being sent to the user.
|
|
// When user clicks on a link it should get the ui with form to
|
|
// enter new password, when form is submitted token and new password
|
|
// must be sent as PUT request to 'password/reset' passwordResetEndpoint
|
|
func passwordResetRequestEndpoint(svc users.Service) endpoint.Endpoint {
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(passwResetReq)
|
|
if err := req.validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
res := passwChangeRes{}
|
|
email := req.Email
|
|
if err := svc.GenerateResetToken(ctx, email, req.Host); err != nil {
|
|
return nil, err
|
|
}
|
|
res.Msg = MailSent
|
|
logger.Info("User made a password reset request")
|
|
return res, nil
|
|
}
|
|
}
|
|
|
|
// This is endpoint that actually sets new password in password reset flow.
|
|
// When user clicks on a link in email finally ends on this endpoint as explained in
|
|
// the comment above.
|
|
func passwordResetEndpoint(svc users.Service) endpoint.Endpoint {
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(resetTokenReq)
|
|
if err := req.validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
res := passwChangeRes{}
|
|
if err := svc.ResetPassword(ctx, req.Token, req.Password); err != nil {
|
|
return nil, err
|
|
}
|
|
res.Msg = ""
|
|
return res, nil
|
|
}
|
|
}
|
|
|
|
func viewUserEndpoint(svc users.Service) endpoint.Endpoint {
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(viewUserReq)
|
|
if err := req.validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
u, err := svc.User(ctx, req.token)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return viewUserRes{
|
|
ID: u.ID,
|
|
Email: u.Email,
|
|
Metadata: u.Metadata,
|
|
}, nil
|
|
}
|
|
}
|
|
|
|
func updateUserEndpoint(svc users.Service) endpoint.Endpoint {
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(updateUserReq)
|
|
if err := req.validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
user := users.User{
|
|
Metadata: req.Metadata,
|
|
}
|
|
err := svc.UpdateUser(ctx, req.token, user)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return updateUserRes{}, nil
|
|
}
|
|
}
|
|
|
|
func passwordChangeEndpoint(svc users.Service) endpoint.Endpoint {
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(passwChangeReq)
|
|
if err := req.validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
res := passwChangeRes{}
|
|
if err := svc.ChangePassword(ctx, req.Token, req.Password, req.OldPassword); err != nil {
|
|
return nil, err
|
|
}
|
|
return res, nil
|
|
}
|
|
}
|
|
|
|
func loginEndpoint(svc users.Service) endpoint.Endpoint {
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(userReq)
|
|
if err := req.validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
token, err := svc.Login(ctx, req.user)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
logger.Info("User logged in")
|
|
return tokenRes{token}, nil
|
|
}
|
|
}
|
|
|
|
func createGroupEndpoint(svc users.Service) endpoint.Endpoint {
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(createGroupReq)
|
|
if err := req.validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
group := users.Group{
|
|
Name: req.Name,
|
|
ParentID: req.ParentID,
|
|
Description: req.Description,
|
|
Metadata: req.Metadata,
|
|
}
|
|
saved, err := svc.CreateGroup(ctx, req.token, group)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
res := createGroupRes{
|
|
ID: saved.ID,
|
|
Name: saved.Name,
|
|
Description: saved.Description,
|
|
Metadata: saved.Metadata,
|
|
ParentID: saved.ParentID,
|
|
created: true,
|
|
}
|
|
logger.Info("Group: " + res.Name + " is created")
|
|
return res, nil
|
|
}
|
|
}
|
|
|
|
func assignUserToGroup(svc users.Service) endpoint.Endpoint {
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(userGroupReq)
|
|
if err := req.validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := svc.Assign(ctx, req.token, req.userID, req.groupID); err != nil {
|
|
return nil, err
|
|
}
|
|
return assignUserToGroupRes{}, nil
|
|
}
|
|
}
|
|
|
|
func removeUserFromGroup(svc users.Service) endpoint.Endpoint {
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(userGroupReq)
|
|
if err := req.validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := svc.Unassign(ctx, req.token, req.userID, req.groupID); err != nil {
|
|
return nil, err
|
|
}
|
|
return removeUserFromGroupRes{}, nil
|
|
}
|
|
}
|
|
|
|
func listUsersForGroupEndpoint(svc users.Service) endpoint.Endpoint {
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(listUserGroupReq)
|
|
if err := req.validate(); err != nil {
|
|
return users.UserPage{}, err
|
|
}
|
|
up, err := svc.Members(ctx, req.token, req.groupID, req.offset, req.limit, req.metadata)
|
|
if err != nil {
|
|
return users.UserPage{}, err
|
|
}
|
|
return buildUsersResponse(up), nil
|
|
}
|
|
}
|
|
|
|
func listUserGroupsEndpoint(svc users.Service) endpoint.Endpoint {
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(listUserGroupReq)
|
|
if err := req.validate(); err != nil {
|
|
return users.UserPage{}, err
|
|
}
|
|
gp, err := svc.Memberships(ctx, req.token, req.userID, req.offset, req.limit, req.metadata)
|
|
if err != nil {
|
|
return groupPageRes{}, err
|
|
}
|
|
return buildGroupsResponse(gp), nil
|
|
}
|
|
}
|
|
|
|
func updateGroupEndpoint(svc users.Service) endpoint.Endpoint {
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(updateGroupReq)
|
|
if err := req.validate(); err != nil {
|
|
return createGroupRes{}, err
|
|
}
|
|
group := users.Group{
|
|
Name: req.Name,
|
|
Description: req.Description,
|
|
Metadata: req.Metadata,
|
|
}
|
|
if err := svc.UpdateGroup(ctx, req.token, group); err != nil {
|
|
return createGroupRes{}, err
|
|
}
|
|
res := createGroupRes{
|
|
Name: group.Name,
|
|
Description: group.Description,
|
|
Metadata: group.Metadata,
|
|
created: false,
|
|
}
|
|
return res, nil
|
|
}
|
|
}
|
|
|
|
func viewGroupEndpoint(svc users.Service) endpoint.Endpoint {
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(groupReq)
|
|
if err := req.validate(); err != nil {
|
|
return viewGroupRes{}, err
|
|
}
|
|
group, err := svc.Group(ctx, req.token, req.groupID)
|
|
if err != nil {
|
|
return viewGroupRes{}, err
|
|
}
|
|
res := viewGroupRes{
|
|
Name: group.Name,
|
|
Description: group.Description,
|
|
Metadata: group.Metadata,
|
|
}
|
|
return res, nil
|
|
}
|
|
}
|
|
|
|
func listGroupsEndpoint(svc users.Service) endpoint.Endpoint {
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(listUserGroupReq)
|
|
if err := req.validate(); err != nil {
|
|
return groupPageRes{}, err
|
|
}
|
|
gp, err := svc.Groups(ctx, req.token, req.groupID, req.offset, req.limit, req.metadata)
|
|
if err != nil {
|
|
return groupPageRes{}, err
|
|
}
|
|
return buildGroupsResponse(gp), nil
|
|
}
|
|
}
|
|
|
|
func deleteGroupEndpoint(svc users.Service) endpoint.Endpoint {
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(groupReq)
|
|
if err := req.validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := svc.RemoveGroup(ctx, req.token, req.groupID); err != nil {
|
|
return nil, err
|
|
}
|
|
return groupDeleteRes{}, nil
|
|
}
|
|
}
|
|
|
|
func buildGroupsResponse(gp users.GroupPage) groupPageRes {
|
|
res := groupPageRes{
|
|
pageRes: pageRes{
|
|
Total: gp.Total,
|
|
Offset: gp.Offset,
|
|
Limit: gp.Limit,
|
|
},
|
|
Groups: []viewGroupRes{},
|
|
}
|
|
for _, group := range gp.Groups {
|
|
view := viewGroupRes{
|
|
ID: group.ID,
|
|
ParentID: group.ParentID,
|
|
Name: group.Name,
|
|
Description: group.Description,
|
|
Metadata: group.Metadata,
|
|
}
|
|
res.Groups = append(res.Groups, view)
|
|
}
|
|
return res
|
|
}
|
|
|
|
func buildUsersResponse(up users.UserPage) userPageRes {
|
|
res := userPageRes{
|
|
pageRes: pageRes{
|
|
Total: up.Total,
|
|
Offset: up.Offset,
|
|
Limit: up.Limit,
|
|
},
|
|
Users: []viewUserRes{},
|
|
}
|
|
for _, user := range up.Users {
|
|
view := viewUserRes{
|
|
ID: user.ID,
|
|
Email: user.Email,
|
|
Metadata: user.Metadata,
|
|
}
|
|
res.Users = append(res.Users, view)
|
|
}
|
|
return res
|
|
}
|