Enable overriding database Models by the calling application - those models must be in the Slack/ subdir

This commit is contained in:
Deon George 2022-05-19 16:29:19 +10:00
parent eb73a67fa8
commit a7f043b23e
9 changed files with 54 additions and 44 deletions

View File

@ -6,10 +6,10 @@ use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use Slack\Models\{Channel,Enterprise,Team,User}; use Slack\Models\{Channel,Enterprise,Team,User};
use App\Models\Channel as AppChannel; use App\Models\Slack\Channel as AppChannel;
use App\Models\Enterprise as AppEnterprise; use App\Models\Slack\Enterprise as AppEnterprise;
use App\Models\Team as AppTeam; use App\Models\Slack\Team as AppTeam;
use App\Models\User as AppUser; use App\Models\Slack\User as AppUser;
/** /**
* Class Base - is a Base to all incoming Slack POST requests * Class Base - is a Base to all incoming Slack POST requests

View File

@ -11,6 +11,10 @@ use Slack\Jobs\TeamUpdate;
use Slack\Models\{Enterprise,Team,Token,User}; use Slack\Models\{Enterprise,Team,Token,User};
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Models\Slack\Channel as AppChannel;
use App\Models\Slack\Enterprise as AppEnterprise;
use App\Models\Slack\Team as AppTeam;
use App\Models\Slack\User as AppUser;
class SlackAppController extends Controller class SlackAppController extends Controller
{ {
@ -84,10 +88,8 @@ class SlackAppController extends Controller
$eo = NULL; $eo = NULL;
if ($output->enterprise) { if ($output->enterprise) {
$eo = Enterprise::firstOrNew( $eo = (class_exists(AppEnterprise::class) ? new AppEnterprise : new Enterprise)
[ ->firstOrNew(['enterprise_id'=>$output->enterprise->id]);
'enterprise_id'=>$output->enterprise->id
]);
$eo->name = $output->enterprise->name; $eo->name = $output->enterprise->name;
$eo->active = TRUE; $eo->active = TRUE;
@ -95,10 +97,8 @@ class SlackAppController extends Controller
} }
// Store our team details // Store our team details
$so = Team::firstOrNew( $so = (class_exists(AppTeam::class) ? new AppTeam : new Team)
[ ->firstOrNew(['team_id'=>$output->team->id]);
'team_id'=>$output->team->id
]);
// We just installed, so we'll make it active, even if it already exists. // We just installed, so we'll make it active, even if it already exists.
$so->description = $output->team->name; $so->description = $output->team->name;
@ -112,7 +112,7 @@ class SlackAppController extends Controller
$to = $so->token; $to = $so->token;
if (! $to) { if (! $to) {
$to = new Token; $to = class_exists(AppToken::class) ? new AppToken : new Token;
$to->description = 'App: Oauth'; $to->description = 'App: Oauth';
} }
@ -125,10 +125,9 @@ class SlackAppController extends Controller
// Create the bot user // Create the bot user
// Store the user who install, and make them admin // Store the user who install, and make them admin
$bo = User::firstOrNew( $bo = (class_exists(AppUser::class) ? new AppUser : new User)
[ ->firstOrNew(['user_id'=>$output->bot_user_id]);
'user_id'=>$output->bot_user_id,
]);
$bo->enterprise_id = $eo ? $eo->id : NULL; $bo->enterprise_id = $eo ? $eo->id : NULL;
$bo->team_id = $so->id; $bo->team_id = $so->id;
$bo->active = 0; $bo->active = 0;
@ -141,10 +140,8 @@ class SlackAppController extends Controller
Log::debug(sprintf('%s:BOT Created [%s]',self::LOGKEY,$bo->id),['m'=>__METHOD__]); Log::debug(sprintf('%s:BOT Created [%s]',self::LOGKEY,$bo->id),['m'=>__METHOD__]);
// Store the user who install, and make them admin // Store the user who install, and make them admin
$uo = User::firstOrNew( $uo = (class_exists(AppUser::class) ? new AppUser : new User)
[ ->firstOrNew(['user_id'=>$output->authed_user->id]);
'user_id'=>$output->authed_user->id,
]);
$uo->enterprise_id = $eo ? $eo->id : NULL; $uo->enterprise_id = $eo ? $eo->id : NULL;
$uo->team_id = $eo ? NULL : $so->id; $uo->team_id = $eo ? NULL : $so->id;

View File

@ -2,6 +2,8 @@
namespace Slack\Models; namespace Slack\Models;
use App\Models\Slack\Team as AppTeam;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
use Slack\Traits\ScopeActive; use Slack\Traits\ScopeActive;
@ -16,7 +18,7 @@ class Channel extends Model
public function team() public function team()
{ {
return $this->belongsTo(Team::class); return $this->belongsTo(class_exists(AppTeam::class) ? AppTeam::class : Team::class);
} }
/* ATTRIBUTES */ /* ATTRIBUTES */
@ -71,4 +73,4 @@ class Channel extends Model
return sprintf('https://%s/archives/%s/p%s',$this->team->url,$this->channel_id,str_replace('.','',$ts)); return sprintf('https://%s/archives/%s/p%s',$this->team->url,$this->channel_id,str_replace('.','',$ts));
} }
} }

View File

@ -2,6 +2,8 @@
namespace Slack\Models; namespace Slack\Models;
use App\Models\Slack\Team as AppTeam;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Slack\Traits\ScopeActive; use Slack\Traits\ScopeActive;
@ -15,6 +17,6 @@ class Enterprise extends Model
public function teams() public function teams()
{ {
return $this->hasMany(Team::class); return $this->hasMany(class_exists(AppTeam::class) ? AppTeam::class : Team::class);
} }
} }

View File

@ -2,6 +2,10 @@
namespace Slack\Models; namespace Slack\Models;
use App\Models\Slack\Channel as AppChannel;
use App\Models\Slack\Token as AppToken;
use App\Models\Slack\User as AppUser;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Slack\API; use Slack\API;
use Slack\Traits\ScopeActive; use Slack\Traits\ScopeActive;
@ -16,34 +20,34 @@ class Team extends Model
public function admins() public function admins()
{ {
return $this->hasMany(User::class,'team_id','id')->where('admin','=',TRUE); return $this->hasMany(class_exists(AppUser::class) ? AppUser::class : User::class,'team_id','id')->where('admin','=',TRUE);
} }
public function bot() public function bot()
{ {
return $this->hasOne(User::class,'id','bot_id'); return $this->hasOne(class_exists(AppUser::class) ? AppUser::class : User::class,'id','bot_id');
} }
public function channels() public function channels()
{ {
return $this->hasMany(Channel::class); return $this->hasMany(class_exists(AppChannel::class) ? AppChannel::class : Channel::class);
} }
public function owner() public function owner()
{ {
return $this->belongsTo(User::class,'admin_id'); return $this->belongsTo(class_exists(AppUser::class) ? AppUser::class : User::class,'admin_id');
} }
// Tokens applicable to this team // Tokens applicable to this team
// @todo team_id can now be null, so we need to get it from the enterprise_id. // @todo team_id can now be null, so we need to get it from the enterprise_id.
public function token() public function token()
{ {
return $this->hasOne(Token::class); return $this->hasOne(class_exists(AppToken::class) ? AppToken::class : Token::class);
} }
public function users() public function users()
{ {
return $this->hasMany(User::class); return $this->hasMany(class_exists(AppUser::class) ? AppUser::class : User::class);
} }
/* ATTRIBUTES */ /* ATTRIBUTES */
@ -92,4 +96,4 @@ class Team extends Model
{ {
return new API($this); return new API($this);
} }
} }

View File

@ -2,6 +2,8 @@
namespace Slack\Models; namespace Slack\Models;
use App\Models\Slack\Team as AppTeam;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Slack\Traits\ScopeActive; use Slack\Traits\ScopeActive;
@ -14,7 +16,7 @@ class Token extends Model
public function team() public function team()
{ {
return $this->belongsTo(Team::class); return $this->belongsTo(class_exists(AppTeam::class) ? AppTeam::class : Team::class);
} }
/* ATTRIBUTES */ /* ATTRIBUTES */
@ -41,4 +43,4 @@ class Token extends Model
{ {
return ($scope AND ($this->getScopesAttribute()->search($scope) !== FALSE)); return ($scope AND ($this->getScopesAttribute()->search($scope) !== FALSE));
} }
} }

View File

@ -2,6 +2,9 @@
namespace Slack\Models; namespace Slack\Models;
use App\Models\Slack\Enterprise as AppEnterprise;
use App\Models\Slack\Team as AppTeam;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use Slack\Traits\ScopeActive; use Slack\Traits\ScopeActive;
@ -18,12 +21,12 @@ class User extends Model
public function enterprise() public function enterprise()
{ {
return $this->belongsTo(Enterprise::class); return $this->belongsTo(class_exists(AppEnterprise::class) ? AppEnterprise::class : Enterprise::class);
} }
public function team() public function team()
{ {
return $this->belongsTo(Team::class); return $this->belongsTo(class_exists(AppTeam::class) ? AppTeam::class : Team::class);
} }
/* ATTRIBUTES */ /* ATTRIBUTES */

View File

@ -8,5 +8,5 @@ return [
'register_notification' => env('SLACK_REGISTER_NOTIFICATION',TRUE), 'register_notification' => env('SLACK_REGISTER_NOTIFICATION',TRUE),
// Our routes that we dont check for signatures // Our routes that we dont check for signatures
'bypass_routes' => ['/','slack-install-button','slack-install'], 'bypass_routes' => ['slack','slack/install','slack/install/button'],
]; ];

View File

@ -6,17 +6,17 @@ $routeConfig = [
app('router') app('router')
->group($routeConfig, function ($router) { ->group($routeConfig, function ($router) {
$router->get('slack-install-button', [ $router->get('slack/install/button', [
'uses' => 'SlackAppController@button', 'uses' => 'SlackAppController@button',
'as' => 'slack-install-button', 'as' => 'slack-install-button',
]); ]);
$router->get('slack-install', [ $router->get('slack/install', [
'uses' => 'SlackAppController@install', 'uses' => 'SlackAppController@install',
'as' => 'slack-install', 'as' => 'slack-install',
]); ]);
$router->get('', [ $router->get('slack', [
'uses' => 'SlackAppController@home', 'uses' => 'SlackAppController@home',
'as' => 'home', 'as' => 'home',
]); ]);
@ -24,18 +24,18 @@ app('router')
app('router') app('router')
->group(array_merge($routeConfig,['prefix'=>'api']), function ($router) { ->group(array_merge($routeConfig,['prefix'=>'api']), function ($router) {
$router->post('event', [ $router->post('slack/event', [
'uses' => 'EventsController@fire', 'uses' => 'EventsController@fire',
'as' => 'event', 'as' => 'event',
]); ]);
$router->post('imsg', [ $router->post('slack/imsg', [
'uses' => 'InteractiveMessageController@fire', 'uses' => 'InteractiveMessageController@fire',
'as' => 'imsg', 'as' => 'imsg',
]); ]);
$router->post('imsgopt', [ $router->post('slack/imsgopt', [
'uses' => 'InteractiveOptionsController@fire', 'uses' => 'InteractiveOptionsController@fire',
'as' => 'imsgopt', 'as' => 'imsgopt',
]); ]);
}); });