Exemple #1
0
        public ServiceResult <IEnumerable <OrganizationMembershipViewModel> > GetUserMemberships()
        {
            var userId = base.AuthenticationInformation.UserId;

            var user = _userRepository.GetMany(P => P.Id == userId).FirstOrDefault();

            if (user == null)
            {
                return new ServiceResult <IEnumerable <OrganizationMembershipViewModel> >()
                       {
                           Message    = new UserNotFoundMessage(),
                           StatusCode = StatusCode.NotFound
                       }
            }
            ;

            List <SuperAdminMembership> superAdminMemberships = _superAdminMembershipRepository.GetMany(P => P.SuperAdmin.Role.UserId == userId).ToList();
            List <AdminMembership>      adminMemberships      = _adminMembershipRepository.GetMany(P => P.Admin.Role.UserId == userId).ToList();
            List <SecretaryMembership>  secretaryMemberships  = _secretaryMembershipRepository.GetMany(P => P.Secretary.Role.UserId == userId).ToList();
            List <CustomerMembership>   customerMemberships   = _customerMembershipRepository.GetMany(P => P.Customer.Role.UserId == userId).ToList();
            List <CollectorMembership>  collectorMemberships  = _collectorMembershipRepository.GetMany(P => P.Collector.Role.UserId == userId).ToList();
            List <DeliveryMembership>   deliveyMemberships    = _deliveryMembershipRepository.GetMany(P => P.Delivery.Role.UserId == userId).ToList();

            IEnumerable <OrganizationMembership> memberships = new List <OrganizationMembership>();

            memberships = memberships.Union(superAdminMemberships);
            memberships = memberships.Union(adminMemberships);
            memberships = memberships.Union(collectorMemberships);
            memberships = memberships.Union(customerMemberships);
            memberships = memberships.Union(deliveyMemberships);
            memberships = memberships.Union(secretaryMemberships);

            var membershipsViewModel = _mapper.Map <IEnumerable <OrganizationMembershipViewModel> >(memberships);

            return(new ServiceResult <IEnumerable <OrganizationMembershipViewModel> >(membershipsViewModel));
        }
    }
        public ServiceResult <string> SecondStepLogin(MembershipAuthenticationBindingModel model)
        {
            var UserId = base.AuthenticationInformation.UserId;

            if (!UserId.HasValue)
            {
                return new ServiceResult <string>()
                       {
                           Message    = new InvalidTokenMessage(),
                           StatusCode = StatusCode.Forbidden
                       }
            }
            ;

            var User = _userRepository.GetMany(P => P.Id == UserId).FirstOrDefault();

            if (User == null)
            {
                return new ServiceResult <string>()
                       {
                           Message    = new InvalidTokenMessage(),
                           StatusCode = StatusCode.NotFound
                       }
            }
            ;

            RoleEnum Role;
            var      RoleConversionResult = Enum.TryParse <RoleEnum>(model.Role, true, out Role);

            if (!RoleConversionResult)
            {
                return new ServiceResult <string>()
                       {
                           Message    = new InvalidRoleMessage(),
                           StatusCode = StatusCode.BadRequest
                       }
            }
            ;

            string Token = "";

            switch (Role)
            {
            case RoleEnum.SuperAdmin:
                SuperAdminMembership SuperAdminMembership = _superAdminMembershipRepository.GetMany(P => P.OrganizationId == model.OrganizationId && P.SuperAdmin.Role.UserId == UserId).FirstOrDefault();

                if (SuperAdminMembership == null)
                {
                    return new ServiceResult <string>()
                           {
                               Message    = new MembershipNotFoundMessage(),
                               StatusCode = StatusCode.NotFound
                           }
                }
                ;

                Token = _jsonWebTokenEngine.GenerateToken(UserId.Value, User.Username, model.OrganizationId, SuperAdminMembership.Organization.Title, Role.ToString("g"));
                break;

            case RoleEnum.Admin:
                AdminMembership AdminMembership = _adminMembershipRepository.GetMany(P => P.OrganizationId == model.OrganizationId && P.Admin.Role.UserId == UserId).FirstOrDefault();
                if (AdminMembership == null)
                {
                    return new ServiceResult <string>()
                           {
                               Message    = new MembershipNotFoundMessage(),
                               StatusCode = StatusCode.NotFound
                           }
                }
                ;

                Token = _jsonWebTokenEngine.GenerateToken(UserId.Value, User.Username, model.OrganizationId, AdminMembership.Organization.Title, Role.ToString("g"));
                break;

            case RoleEnum.Secretary:
                SecretaryMembership SecretaryMembership = _secretaryMembershipRepository.GetMany(P => P.OrganizationId == model.OrganizationId && P.Secretary.Role.UserId == UserId).FirstOrDefault();
                if (SecretaryMembership == null)
                {
                    return new ServiceResult <string>()
                           {
                               Message    = new MembershipNotFoundMessage(),
                               StatusCode = StatusCode.NotFound
                           }
                }
                ;

                Token = _jsonWebTokenEngine.GenerateToken(UserId.Value, User.Username, model.OrganizationId, SecretaryMembership.Organization.Title, Role.ToString("g"));
                break;

            case RoleEnum.Collector:
                CollectorMembership CollectorMembership = _collectorMembershipRepository.GetMany(P => P.OrganizationId == model.OrganizationId && P.Collector.Role.UserId == UserId).FirstOrDefault();
                if (CollectorMembership == null)
                {
                    return new ServiceResult <string>()
                           {
                               Message    = new MembershipNotFoundMessage(),
                               StatusCode = StatusCode.NotFound
                           }
                }
                ;

                Token = _jsonWebTokenEngine.GenerateToken(UserId.Value, User.Username, model.OrganizationId, CollectorMembership.Organization.Title, Role.ToString("g"));
                break;

            case RoleEnum.Delivery:
                DeliveryMembership DeliveryMembership = _deliveryMembershipRepository.GetMany(P => P.OrganizationId == model.OrganizationId && P.Delivery.Role.UserId == UserId).FirstOrDefault();
                if (DeliveryMembership == null)
                {
                    return new ServiceResult <string>()
                           {
                               Message    = new MembershipNotFoundMessage(),
                               StatusCode = StatusCode.NotFound
                           }
                }
                ;

                Token = _jsonWebTokenEngine.GenerateToken(UserId.Value, User.Username, model.OrganizationId, DeliveryMembership.Organization.Title, Role.ToString("g"));
                break;

            case RoleEnum.Customer:
                CustomerMembership CustomerMembership = _customerMembershipRepository.GetMany(P => P.OrganizationId == model.OrganizationId && P.Customer.Role.UserId == UserId).FirstOrDefault();
                if (CustomerMembership == null)
                {
                    return new ServiceResult <string>()
                           {
                               Message    = new MembershipNotFoundMessage(),
                               StatusCode = StatusCode.NotFound
                           }
                }
                ;

                Token = _jsonWebTokenEngine.GenerateToken(UserId.Value, User.Username, model.OrganizationId, CustomerMembership.Organization.Title, Role.ToString("g"));
                break;

            default:
                break;
            }

            return(new ServiceResult <string>(Token));
        }
    }
}