定制 networkrailbusinesssystems/entra 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

networkrailbusinesssystems/entra

Composer 安装命令:

composer require networkrailbusinesssystems/entra

包简介

Easily sign-in and poll users and groups in Microsoft Entra

README 文档

README

Composer status Coverage status Laravel version PHP version Tests status

Easily sign-in and poll users and groups in Microsoft Entra, built using Laravel Microsoft Graph.

Setup

  1. Install this library using Composer:
    composer require networkrailbusinesssystems/entra
  2. Publish the Entra configuration file using Artisan:
    php artisan vendor:publish --tag="entra"
  3. Publish the MsGraph configuration file
    php artisan vendor:publish --provider="Dcblogdev\MsGraph\MsGraphServiceProvider" --tag="config"
  4. Publish the MsGraph migration file
    php artisan vendor:publish --provider="Dcblogdev\MsGraph\MsGraphServiceProvider" --tag="migrations"
    • Consider adding a foreign key to the user_id column for greater efficiency
  5. Adjust the entra.php configuration file to suit your needs
  6. Setup your authenticatable User model
    • Implement the EntraAuthenticatable interface on your chosen Model
    • Add the AuthenticatesWithEntra trait on your chosen Model for a standard fetch and sync setup, or implement the methods yourself
  7. Add the Entra authentication routes to your routes/web.php using the macro:
    Route::entra();
    
    Route::middleware('EntraAuthenticated')->group(function () {
        // Your authenticated routes here...
    }

Configuration

create_users

Whether to allow Users without an account in the system already to sign-in.

When set to true, Entra will automatically create a new User record for anyone who successfully signs in.

When set to false, Users must be manually added before they can sign-in, even with a valid SSO session.

messages

Customise any of the error messages thrown by Entra.

Key Usage
existing_only Shown when a new User attempts to log into a system with create_users set to false

sync_attributes

Any attributes set here will automatically be filled when signing in, keeping the User up to date with any changes in Entra.

The array should contain a key-value pair in entra => laravel format.

'sync_attributes' => [
    'mail' => 'email',
],

user_model

The fully qualified class name of the Model used for Laravel authentication.

use App/Models/User;

'user_model' => User::class,

emulator

These attributes controls the Entra emulator, and the mock data available to it.

See the "Emulator" section further on for more information.

Signing in and out

Users will automatically be redirected to the Microsoft Azure login page whenever they attempt to access an authenticated route as a guest.

The EntraServiceProvider automatically registers the relevant event listeners for authentication.

Automatic

If you wrap all of your system's endpoints in the EntraAuthenticated middleware, Users will be automatically kicked to the Entra login page.

Should they become signed out for whatever reason, they will be kicked to the Entra login screen.

This may or may not be desirable based on how much of the system should be available to non-users.

Manual sign-in and out

You can allow users to manually login by providing a link to the login route, which will take them to the Entra login page.

Users can logout by calling the logout route, which will take them to the Entra logout page.

Middleware

The EntraAuthenticated middleware has been provided to replace the base MsGraphAuthenticated middleware.

This works in the same way as the base middleware, adding support for Laravel's intended destination route handling.

Querying Entra

You can use the Laravel Microsoft Graph library as normal.

Entra queries on routes outside of the EntraAuthenticated middleware must connect to Entra first, otherwise the request will hit a 302 redirect and fail.

Service Accounts

Calling MsGraph will access Entra using the currently signed-in user's credentials.

Calling MsGraphAdmin will access Entra as the application, without a signed-in user.

MsGraphAdmin should be used for any calls to Entra which are performed in the background, such as queues, jobs, and commands.

Any calls using the models provided by this library will automatically fall back to the MsGraphAdmin account if there is no signed-in user.

MsGraph

A drop-in alias for the MsGraph facade has been provided which adds docblocks for IDE support.

The connect method has been partially overridden to add support for Laravel's inbuilt intended route handling.

MsGraphAdmin

A drop-in alias for the MsGraphAdmin facade has been provided which adds docblocks for IDE support.

EntraGroup

Get

Search for and return a specific group.

Parameter Type Default Usage
$term string A unique string to find the group by
$field string mail Which field to look for the $term in
$select ?array [] Which fields to return
:returns ?array The group as an array, or null

List

Search for groups which start with a term.

Parameter Type Default Usage
$term string A unique string to find the group by
$field string mail Which field to look for the $term in
$limit int 10 How many results to show
$select ?array [] Which fields to return
:returns array An array of groups

EntraGroupMembers

Get

Retrieve a list of users for a group by the group's ID.

Entra will be polled until all of the group's users have been loaded.

Parameter Type Default Usage
$term string A unique string to find the group by
$field string mail Which field to look for the $term in
$select ?array ['mail'] Which fields to return
:returns ?array The group's members as an array, or null

EntraUser

Get

Search for and return a specific user.

Parameter Type Default Usage
$term string A unique string to find the user by
$field string mail Which field to look for the $term in
$select ?array config(entra.sync_attributes) Which fields to return
:returns ?array The user as an array, or null

List

Search for users which start with a term.

Parameter Type Default Usage
$term string A unique string to find the user by
$field string mail Which field to look for the $term in
$limit int 10 How many results to show
$select ?array config(entra.sync_attributes) Which fields to return
:returns array An array of users

Import

Search for and import a specific user to the database.

If the user already exists they will be updated.

Parameter Type Default Usage
$term string A unique string to find the user by
$field string mail Which field to look for the $term in
$select ?array config(entra.sync_attributes) Which fields to return
:returns ?EntraAuthenticatable The user model, or null

Commands

The following Artisan commands are available for use:

Command Parameters Usage
entra:import-user $term, $entraField = 'mail' Import a single User by the given term
entra:refresh-users $laravelField = 'email', $entraField = 'mail' Re-import all Users in the system by a given field

Rules

UserExistsInEntra

Ensure that the given User exists in Entra.

// Request data
[
    'email' => 'joe.bloggs@networkrail.co.uk',
];

// FormRequest rules()
'email' => [
    new UserExistsInEntra('mail'),
];

You may provide the field to match the value to as the first parameter of the Rule.

Emulator

It is unlikely that your unit tests will ever be connected to a live Entra instance.

You can mock MsGraph for specific calls, however you may prefer to re-use a defined list of results.

The emulator only works with the models provided by this library.

Setting entra.emulator.enabled = true in your config will enable the emulator.

You may also use the AssertsEntra trait on your base TestCase class to make the useEntraEmulator() method available in your tests.

Emulation does not support signing in or out.

EntraGroup and EntraGroupMembers

Defining a list of groups on the entra.emulator.groups key will allow you to create a custom list of groups with members which you can re-use.

Performing an EntraGroup::get will return a matching group from the list.

Performing an EntraGroup::list will return a matching set of results from the list.

Performing an EntraGroupMembers::get will return a matching group's members from the list.

EntraUser

Defining a list of users on the entra.emulator.users key will allow you to create a custom list of users which you can re-use.

Performing an EntraUser::get or EntraUser::import will return a matching user from the list.

Performing an EntraUser::list will return a matching set of results from the list.

Sample Entra responses

Sample responses are provided on the relevant EntraModel.

Roadmap

  • Add 302 handler to MsGraph facade

Help and support

You are welcome to raise any issues or questions on GitHub.

If you wish to contribute to this library, raise an issue before submitting a forked pull request.

Licence

Published under the MIT licence.

networkrailbusinesssystems/entra 适用场景与选型建议

networkrailbusinesssystems/entra 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 554 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 02 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「graph」 「auth」 「laravel」 「microsoft」 「EntraAuthenticatable」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 networkrailbusinesssystems/entra 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 networkrailbusinesssystems/entra 我们能提供哪些服务?
定制开发 / 二次开发

基于 networkrailbusinesssystems/entra 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 554
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 26
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-02-04