Skip to content

Commit

Permalink
Rework and upgrade project to Laravel 9
Browse files Browse the repository at this point in the history
  • Loading branch information
miracuthbert committed Dec 14, 2022
1 parent 69b7f4f commit 5d9bca0
Show file tree
Hide file tree
Showing 284 changed files with 12,427 additions and 5,356 deletions.
23 changes: 18 additions & 5 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,24 @@ APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
DB_USERNAME=root
DB_PASSWORD=

BROADCAST_DRIVER=log
CACHE_DRIVER=file
FILESYSTEM_DISK=local
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120
QUEUE_DRIVER=sync

MEMCACHED_HOST=127.0.0.1

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
Expand All @@ -29,13 +34,21 @@ MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS="[email protected]"
MAIL_FROM_NAME="${APP_NAME}"

MAIL_FROM_ADDRESS=
MAIL_FROM_NAME=
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_HOST=
PUSHER_PORT=443
PUSHER_SCHEME=https
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
Expand Down
8 changes: 8 additions & 0 deletions .rnd
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
M�F�N����g����Iu��� ���Jq;��yZ�WoPz��>-�;cӌ0:�<I��w��~=�^Г�t�ka2-g� /1�8B�բ�� �ɜ�F�
8�jK�������Il���F��/�
�|�ր�ȸ�X�a?��o\���03,zÄ�}{�>����f�q[1A�P�<��RV ����t(־f{�?���$B�ݰb����r��O�����N\�堐 ��ߵ��`��V;���ę����xa�R4����\>�9.ٗ���l�
ɴ�O&�w�0fh5�de�v�ȶ��<��b�p,xs_@�;c��ޑ�d�|���0�Q��T�駉�ӢA)�+�Z�EjW����|Di^\ÿF� %�M5����$
!g`��XB��H�a��!ne�|�1������n�M�+�7���!�M �qR]����*�T�l\�K ¼9���1�9V<rY>��4G)UzW���<���L��Ȗmm�� ���X���]��э�9\~�u�]��,���G�i�D�"��T�xB�h]�~�K�tQ$�&A/gH�����T�P�VB��9�웅*@E�����f9X�����(������+P�Rh�
������a������} +O���L���ɇR*'�'�9hCn,�9�d�z�K���ry�*Q�E�ԇ.�� Ng��u���qٟ�8cK7���\Sߎ��?&�����[�A����76`<#C<t�*�l"H�@�X:�4+������(�!����}c)?f,㡮[��!Q�H�Aw}��!�{��VzG�Kg?���@d��W�/
�j�C��"6SƔA!1#� �|t]�U�"�@� �Ɛ����^�Sl��C���ͯ��Sׯ����תK����&����ۓMiG��/�lv9�'�M�?Q �Ut�k� se�1 ��˥$��Fk� ���<��!V�� 獝�ʰSI�w�p�
Cpzb���U3�E��h����ə�ֱ�Ķul�:,��9�m��
Expand Down
145 changes: 84 additions & 61 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,87 +1,96 @@
# saas-boilerplate
This is a SaaS boilerplate built on top of the Laravel framework.

This is a SaaS boilerplate built on top of the Laravel framework.
Built to provide developers with a template to kickoff their SaaS application,
without the hustle for repetitive tasks such as user account setup, subscriptions
without the hustle for repetitive tasks such as user account setup, subscriptions
and role management.

## features

- Authentication
- Login / Registration
- Email Activation
- Two Factor Login (only when enabled)
- Login / Registration
- Email Activation
- Two Factor Login (only when enabled)
- Subscription with Stripe
- User plans
- Team plans
- User plans
- Team plans
- Account (User Account)
- Profile Update
- Change Password
- Two Factor Authentication
- Subscription
- Cancel Subscription
- Resume Subscription
- Swap Plan
- Update Card
- API Tokens
- Profile Update
- Change Password
- Two Factor Authentication
- Subscription
- Cancel Subscription
- Resume Subscription
- Swap Plan
- Update Card
- API Tokens
- Single Database Multi-tenancy
- Admin
- User Management
- Manage User Roles
- Role & Permissions Management
- User Management
- Create Users (and assign role)
- Manage User Roles
- Role & Permissions Management
- Developer Panel
- Manage OAuth Clients
- Manage Personal Access Tokens
- Manage OAuth Clients
- Manage Personal Access Tokens
- Other features
- Filtering (extendable)
- API access (starter template)
- Filtering (extendable)
- API access (starter template)

*Note: Some features may be subjected to change. Other features may not be listed
since they are under development or do not qualify as a standard / main SaaS feature.
*Note: Some features may be subjected to change. Other features may not be listed
since they are under development or do not qualify as a standard / main SaaS feature.
Some common features will not be listed as well.*

## installation

1. Fork, clone or download this repository.
2. Run `composer install` if its the initial setup or `composer update`.
3. Setup your environment keys in .env
3. Setup your environment keys in .env
(*If .env does not exist then copy / rename .env.example as .env*)
4. Run `php artisan app:name` to set the name (namespace) of your app.
(*Remember not to live any spaces*)
5. Run `php artisan migrate` for initial tables setup.
6. __Optional:__ Run `php artisan db:seed --class=RoleTableSeeder` to set the initial
4. Run `php artisan migrate` for initial tables setup.
5. __Optional:__ Run `php artisan db:seed --class=RoleTableSeeder` to set the initial
roles and permissions, then follow `step 7` below to assign a user the initial permissions and roles.
7. __Optional:__ To create a `super / root` admin;
Run `php artisan admin:assign-role [email protected] admin-root`.
Substitute `[email protected]` with an existing user email. `admin-root` is the __default root Admin role__.

__Note:__ You must follow `step 6` above first to setup the root admin.
6. __Optional:__ To create a `super / root` admin;
Run `php artisan role:assign [email protected] admin-root`.
Substitute `[email protected]` with an existing user email. `admin-root` is the __default root Admin role__.

__Note:__ You must follow `step 5` above first to setup the root admin.

## usage

### Custom Commands
1. __Admin: _Assign user a role___
- Use `php artisan admin:assign-role <email> <role-slug>`:
`<email>` is the user's email and `<role-slug>` is the _slug of the role_ you wish to assign the user.

1. __Admin: *Assign user a role*__
- Use `php artisan role:assign <email> <role-slug>`:
`<email>` is the user's email and `<role-slug>` is the *slug of the role* you wish to assign the user.

### Force HTTPS
When pushing the project to a platform or production environment,

When pushing the project to a platform or production environment,
assets or urls may be broken if the platform enforces HTTPS.

To enable urls to use HTTPS:

Set `FORCE_HTTPS` variable in `.env` file to `true`.
*To enable urls to use HTTPS:*

Set `FORCE_HTTPS` variable in `.env` file to `true`.

By default `FORCE_HTTPS` is `false`.

```Note: ``` If `FORCE_HTTPS` does not exist in your `.env`,
```Note:``` If `FORCE_HTTPS` does not exist in your `.env`,
just add it as a new variable and assign a boolean value `true` or `false`.

This dynamically tells Laravel to force urls to use HTTPS which is especially
This dynamically tells Laravel to force urls to use HTTPS which is especially
handy in fixing or preventing broken assets urls.

### Single Database Multi-tenancy

See [miracuthbert/laravel-multi-tenancy](https://github.com/miracuthbert/laravel-multi-tenancy)

#### Model setup

To start using single databse multi-tenancy call `ForTenants` trait on a model

```php
use SAASBoilerplate\App\Tenant\Traits\ForTenants;
use Miracuthbert\Multitenancy\Traits\ForTenants;

class Project extends Model
{
Expand All @@ -90,7 +99,8 @@ class Project extends Model
```

#### Tenants CRUD Operations
Once you have setup the model as show above. `Just call CRUD operations directly`.

Once you have setup the model as show above. `Just call CRUD operations directly`.
`Tenant` relationships are handled automatically.

```php
Expand All @@ -101,38 +111,51 @@ $projects = Project::get();
```

#### Normal CRUD Operations
To perform CRUD operations on models with `ForTenants` trait can be done by

To perform CRUD operations on models with `ForTenants` trait can be done by
adding `withoutForTenants` scope when fetching records associated with that model.

```php
$projects = Project::withoutForTenants()->get();
```

***This comes in handy for example in: admin or moderation operations***
*__This comes in handy for example in: admin or moderation operations__*

#### Routing

All `tenant` routes are under the routes folder in the `tenant.php` file.

Note: ***Tenant routes follow the same structure as other routes***
Note: *__Tenant routes follow the same structure as other routes__*

`The main reason we place all tenant routes separately is to handle route binding and
its much easier to know which routes are for tenants.`

## libraries & packages

- Main
- PHP (>=7.1.3)
- Laravel (Minimal 5.6)
- Laravel Cashier (can be switched)
- PHP (>=8)
- Laravel (Minimal 9)
- Laravel Cashier (can be switched)
- UI (can be switched)
- Bootstrap (v4)
- Font awesome
- Simple Line Icons
- jQuery
- VueJs
- Development
- nodejs
- npm
- Bootstrap (v4)
- Font awesome
- Simple Line Icons
- jQuery
- VueJs
- Development
- nodejs
- npm

## services

- Stripe (payment gateway)
- Authy by Twilio (two factor authentication)
- Authy by Twilio (two factor authentication)

## Changes

- `Roles and Permissions`: See [miracuthbert/laravel-roles](https://github.com/miracuthbert/laravel-roles)
- `Multi-tenancy`: See [miracuthbert/laravel-multi-tenancy](https://github.com/miracuthbert/laravel-multi-tenancy)

## Security Vulnerabilities

If you discover a security vulnerability, please send an e-mail to Cuthbert Mirambo via [[email protected]](mailto:[email protected]). All security vulnerabilities will be promptly addressed.
78 changes: 78 additions & 0 deletions app/App/Actions/Fortify/CreateNewUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace SAAS\App\Actions\Fortify;

use Illuminate\Support\Arr;
use Illuminate\Validation\Rule;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use SAAS\Domain\Users\Models\Role;
use SAAS\Domain\Users\Models\User;
use Laravel\Fortify\Contracts\CreatesNewUsers;
use Illuminate\Auth\Events\Registered;

class CreateNewUser implements CreatesNewUsers
{
use PasswordValidationRules;

protected $defaults = [
'password' => 'password',
'confirmation_password' => 'password',
];

/**
* Validate and create a newly registered user.
*
* @param array $input
* @param array $except
* @param bool $fireEvent
* @return \SAAS\Domain\Users\Models\User
*/
public function create(array $input, $except = [], $fireEvent = false)
{
$input = array_merge($input, Arr::only($this->defaults, $except));

Validator::make($input, Arr::except([
'first_name' => 'required|string|max:30',
'last_name' => 'required|string|max:30',
'username' => 'nullable|string|max:30|unique:users',
'email' => [
'required',
'string',
'email',
'max:255',
Rule::unique(User::class),
],
'password' => $this->passwordRules(),
'terms' => 'required'
], $except))->validate();

return DB::transaction(function () use ($input, $fireEvent) {
return tap(User::create([
'first_name' => $input['first_name'],
'last_name' => $input['last_name'],
'username' => $input['username'],
'email' => $input['email'],
'password' => Hash::make($input['password']),
]), function (User $user) use($input, $fireEvent) {
if ($fireEvent) {
$this->fireEvent($user, $fireEvent);
}
$this->assignRole($user, $input['role_id'] ?? null);
});
});
}

public function fireEvent(User $user, $fireEvent = false)
{
event(new Registered($user));
}

public function assignRole(User $user, $roleId)
{
if ($roleId && ($role = Role::find($roleId))) {
$user->assignRole($role);
}
}
}
18 changes: 18 additions & 0 deletions app/App/Actions/Fortify/PasswordValidationRules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace SAAS\App\Actions\Fortify;

use Laravel\Fortify\Rules\Password;

trait PasswordValidationRules
{
/**
* Get the validation rules used to validate passwords.
*
* @return array
*/
protected function passwordRules()
{
return ['required', 'string', new Password, 'confirmed'];
}
}
Loading

0 comments on commit 5d9bca0

Please sign in to comment.