├── .styleci.yml ├── CHANGELOG.md ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE.md ├── README.md ├── composer.json ├── public └── images │ └── sharedo.gif └── src ├── Controllers └── SharedoController.php ├── Events └── UserAbilityChanged.php ├── Interfaces └── UserContactsInterface.php ├── Listeners └── SendUserAbilityChangedNotification.php ├── Middleware ├── RestrictEntities.php └── ShareInertiaData.php ├── Notifications └── UserInvitedNotificaton.php ├── Services ├── AssignAbilityService.php ├── InvitedUsersService.php └── RemovePreviousAbiltiesService.php ├── SharedoPreset.php ├── SharedoServiceProvider.php ├── config └── config.php ├── migrations └── 2021_02_18_072401_create_new_users_sharedo_table.php ├── resources ├── js │ ├── Sharedo │ │ └── Pages │ │ │ ├── Access.vue │ │ │ ├── FlashMessage.vue │ │ │ ├── Layout.vue │ │ │ ├── Owner.vue │ │ │ ├── Tag.vue │ │ │ ├── Users.vue │ │ │ └── index.vue │ └── sharedo.js └── views │ ├── mail │ └── invite-user.blade.php │ └── sharedo.blade.php └── routes └── web.php /.styleci.yml: -------------------------------------------------------------------------------- 1 | preset: laravel 2 | 3 | disabled: 4 | - single_class_element_per_statement 5 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `sharedo` will be documented in this file 4 | 5 | ## 1.0.0 - 201X-XX-XX 6 | 7 | - initial release 8 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, gender identity and expression, level of experience, 9 | nationality, personal appearance, race, religion, or sexual identity and 10 | orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | - Using welcoming and inclusive language 18 | - Being respectful of differing viewpoints and experiences 19 | - Gracefully accepting constructive criticism 20 | - Focusing on what is best for the community 21 | - Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | - The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | - Trolling, insulting/derogatory comments, and personal or political attacks 28 | - Public or private harassment 29 | - Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | - Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at strapui-support@sahusoft.com. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at [http://contributor-covenant.org/version/1/4][version] 72 | 73 | [homepage]: http://contributor-covenant.org 74 | [version]: http://contributor-covenant.org/version/1/4/ 75 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | We want to make contributing to this project as easy and transparent as possible and we are grateful for, any contributions made by the community. By contributing to sharedo, you agree to abide by the [code of conduct](https://github.com/GeekyAnts/laravel-inertia-sharedo/blob/master/CONTRIBUTING.md). 4 | 5 | ## Reporting Issues and Asking Questions 6 | 7 | Before opening an issue, please search the [issue tracker](https://github.com/GeekyAnts/laravel-inertia-sharedo/issues) to make sure your issue hasn't already been reported. 8 | 9 | ## Bugs and Improvements 10 | 11 | We use the issue tracker to keep track of bugs and improvements to sharedo itself, its repo, and the documentation. We encourage you to open issues to discuss improvements, architecture, theory, internal implementation, etc. If a topic has been discussed before, we will ask you to join the previous discussion. 12 | 13 | Please read and understand the contribution guide before creating an issue or pull request. 14 | 15 | ## Docs 16 | 17 | Improvements to the documentation are always welcome. You can find them in the on [sharedo-Docs](https://github.com/GeekyAnts/laravel-sharedo-docs) repository. We use [Docusaurus](https://v2.docusaurus.io/) to build our documentation website. The website is published automatically whenever the `master` branch is updated. 18 | 19 | ## Sending a Pull Request 20 | 21 | For non-trivial changes, please open an issue with a proposal for a new feature or refactoring before starting on the work. We don't want you to waste your efforts on a pull request that we won't want to accept. 22 | 23 | In general, the contribution workflow looks like this: 24 | 25 | - Open a new issue in the [Issue tracker](https://github.com/GeekyAnts/laravel-inertia-sharedo/issues). 26 | - Fork the repo. 27 | - Create a new feature branch based off the `master` branch. 28 | - Submit a pull request, referencing any issues it addresses. 29 | 30 | Please try to keep your pull request focused in scope and avoid including unrelated commits. 31 | 32 | After you have submitted your pull request, we'll try to get back to you as soon as possible. We may suggest some changes or improvements. 33 | 34 | Thank you for contributing! 35 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) GeekyAnts 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # **Sharedo** 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/geekyants/sharedo.svg?style=flat-square)](https://packagist.org/packages/geekyants/sharedo) 4 | [![License](https://img.shields.io/packagist/l/geekyants/sharedo)](https://github.com/GeekyAnts/laravel-inertia-sharedo/blob/master/LICENSE.md) 5 | [![Total Downloads](https://img.shields.io/packagist/dt/geekyants/sharedo.svg?style=flat-square)](https://packagist.org/packages/geekyants/sharedo) 6 | 7 | Share Popup for Laravel Apps 8 | 9 | ![Sharedo](./public/images/sharedo.gif) 10 | 11 | ## 1) Introduction 12 | 13 | **Sharedo** provides you an interface to manage roles and permissions for Laravel apps. You can assign read or write permissions to a user and remove the permissions as required. It is powered by Inertia.js and Laravel Bouncer. 14 | 15 | ## 2) Motivation 16 | 17 | We realised that it is very time consuming to add a share functionality to Laravel projects and this can hinder the development process. 18 | This package aims to solve this problem by enabling you to share your project's entities with other users easily. 19 | 20 | ## 3) Dependencies 21 | 22 | If you have [Tailwind](https://tailwindcss.com/) and [Bouncer](https://github.com/JosephSilber/bouncer) pre-installed, you can move on to the [Installation](#4-installation) section. 23 | 24 | ### **Tailwind Installation** 25 | 26 | Install Tailwind as shown below: 27 | 28 | ```jsx 29 | npm install -D tailwindcss@latest postcss@latest autoprefixer@latest 30 | ``` 31 | 32 | Next, generate your tailwind.config.js file: 33 | 34 | ```jsx 35 | npx tailwindcss init 36 | ``` 37 | 38 | In your **tailwind.config.js** file, configure the **purge** option with the paths to all of your Blade templates and JavaScript components so Tailwind can tree-shake unused styles in production builds: 39 | 40 | ```jsx 41 | module.exports = { 42 | purge: [ 43 | "./resources/**/*.blade.php", 44 | "./resources/**/*.js", 45 | "./resources/**/*.vue", 46 | ], 47 | darkMode: false, // or 'media' or 'class' 48 | theme: { 49 | extend: {}, 50 | }, 51 | variants: { 52 | extend: {}, 53 | }, 54 | plugins: [], 55 | }; 56 | ``` 57 | 58 | In your webpack.mix.js, add **tailwindcss** as a PostCSS plugin: 59 | 60 | ```jsx 61 | mix.js("resources/js/app.js", "public/js").postCss( 62 | "resources/css/app.css", 63 | "public/css", 64 | [require("tailwindcss")] 65 | ); 66 | ``` 67 | 68 | Open the `./resources/css/app.css` file and add the following code, replacing the original file contents: 69 | 70 | ```jsx 71 | /* ./resources/css/app.css */ 72 | @tailwind base; 73 | @tailwind components; 74 | @tailwind utilities; 75 | 76 | ``` 77 | 78 | Next, import your stylesheet in your main Blade layout (commonly `resources/views/layouts/app.blade.php` or similar) and add the responsive viewport meta tag if it's not already present: 79 | 80 | ```jsx 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | ``` 90 | 91 | ### **Bouncer Installation** 92 | 93 | Install Bouncer with composer: 94 | 95 | ```jsx 96 | composer require silber/bouncer v1.0.0-rc.10 97 | ``` 98 | 99 | Add Bouncer's trait to your user model: 100 | 101 | ```jsx 102 | use Silber\Bouncer\Database\HasRolesAndAbilities; 103 | 104 | class User extends Model 105 | { 106 | use HasRolesAndAbilities; 107 | } 108 | ``` 109 | 110 | Publish Bouncer's migrations to your app's migrations directory by running the following command: 111 | 112 | ```jsx 113 | php artisan vendor:publish --tag="bouncer.migrations" 114 | ``` 115 | 116 | ## 4) Installation 117 | 118 | 1. Install the Sharedo package using composer as shown below: 119 | 120 | ```jsx 121 | composer require geekyants/sharedo 122 | ``` 123 | 124 | 1. After installation, move the package's config file to your project's config folder: 125 | 126 | ```jsx 127 | php artisan vendor:publish --tag="config" 128 | ``` 129 | 130 | 1. Moving forward, scaffold the view components present in the sharedo package as follows: 131 | 132 | ```jsx 133 | php artisan ui sharedo 134 | ``` 135 | 136 | A **Sharedo** folder containing Vue.js components will be created in your resources directory. You can now easily customise your Sharedo's Vuejs components 🚀 137 | 138 | 1. Now, run the migrations. After executing this command, Bouncer migrations and the **new_users_sharedo** table will be migrated: 139 | 140 | ```jsx 141 | php artisan migrate 142 | ``` 143 | 144 | 5) To compile and minify the CSS and JavaScript files generated by sharedo, add this to your webpack.mix.js file: 145 | 146 | ```jsx 147 | .js("resources/js/sharedo.js", "public/js") 148 | .vue() 149 | ``` 150 | 151 | > Note: If your css is not compiled in your app.css file, you can change it in the sharedo.blade.php file. 152 | 153 | 6) Install the dependencies: 154 | 155 | ```jsx 156 | composer install 157 | npm install 158 | ``` 159 | 160 | 7) Finally, build your assets as shown below: 161 | 162 | ```jsx 163 | npm run dev 164 | ``` 165 | 166 | ## 5) Usage 167 | 168 | You must define a relation **user** on the entity model that you want to share. The relation should return the user of that entity. 169 | 170 | To share your entity with other users, visit: 171 | 172 | ```jsx 173 | {APP_URL}/sharedo/{entity_name}/{entity_id} 174 | ``` 175 | 176 | For example, if you want to open Sharedo for a project model with id 123, then visit: 177 | 178 | ``` 179 | {APP_URL}/sharedo/projects/123 180 | ``` 181 | 182 | > Note: **The entity_name should have the same name as that of the database migration corresponding to the model that you want to share.** 183 | 184 | > Note: **Sharedo sends error messages back to your application in the error props.** 185 | 186 | If you invite a user who is not present in your database, Sharedo automatically creates it in your users table. Also, a new entry is inserted into the **new_users_sharedo table** referencing the user's id as a foreign key and **has_ever_logged_in** property is set to false. 187 | This can help you differentiate between the users created by Sharedo and users created by the usual sign-up flow. 188 | 189 | To restrict other users from accessing your entities, you have to explicitly use the Bouncer methods [here](https://github.com/JosephSilber/bouncer#cheat-sheet). 190 | 191 | > Note: **Uses who are given write access to the entity can also share that entity with other users.** 192 | 193 | ## 6) Customisation 194 | 195 | You can customise the functionality of Sharedo easily by making changes in the sharedo.php file present in your config folder. 196 | 197 | You can customise the functionality of Sharedo easily by making changes to the **sharedo.php** file present in your config folder. 198 | 199 | 1. If your files are not present in the `App\Models\\`, then change the modelPath to that location instead of `App\Models\\`. 200 | 201 | ```jsx 202 | "modelPath" => "App\Models\\" 203 | ``` 204 | 205 | 2. To add your own custom middleware to Sharedo, append it to the middleware array. 206 | For example, if you want to add the **"admin"** middleware, then your middleware array will look like this: 207 | 208 | ```jsx 209 | "middleware" => ['web', 'auth','admin'] 210 | ``` 211 | 212 | 3. If you want only certain entities to be shareable, you can add them to the  **restrict-entities** array. 213 | For instance, if you want only the files entity to be shareable, your array will look like this: 214 | 215 | ```jsx 216 | 'restrict-entities' => ['files'], 217 | ``` 218 | 219 | 4. You can also send email notifications to the users when they are given access to an entity. 220 | Sharedo fires an **UserAbilityChanged** event when a user's access is changed and attaches the **SendUserAbilityChangedNotification** listener to it. If you want to send an email notification, make the following changes in EventServiceProvider: 221 | 222 | ```php 223 | use Geekyants\Sharedo\Events\UserAbilityChanged; 224 | use Geekyants\Sharedo\Listeners\SendUserAbilityChangedNotification; 225 | protected $listen = [ 226 | ... 227 | UserAbilityChanged::class => [ 228 | SendUserAbilityChangedNotification::class, 229 | ] 230 | 231 | ]; 232 | ``` 233 | 234 | You can also modify the email template by publishing the Sharedo mail resources: 235 | 236 | ```php 237 | php artisan vendor:publish --tag="mail" 238 | ``` 239 | 240 | > After running this command, the mail notification template will be located in the `resources/vendor/sharedo/mail` directory: 241 | 242 | You can attach your own listeners to the event. For example, if you want to attach a **SendSlackNotification** listener to the **UserAbilityChanged** event, execute the following: 243 | 244 | ```php 245 | use Geekyants\Sharedo\Events\UserAbilityChanged; 246 | 247 | ... 248 | 249 | protected $listen = [ 250 | UserAbilityChanged::class => [ 251 | SendSlackNotification::class, 252 | ] 253 | ]; 254 | ``` 255 | 256 | 5. To provide an option that allows searching for users, you can create a class that implements **UserContactsInterface**.php from the package and define the **getUserContacts** function. 257 | 258 | The return type of the **getUserContacts** function should be a string containing a JSON representation of an array of objects. Each object denoting a user must have an **"email"** attribute as shown below: 259 | 260 | ```php 261 | "[ 262 | { email: '', ... }, 263 | { email: '', ... }, 264 | { email: '', ... } 265 | ]" 266 | ``` 267 | 268 | For example, you can create a class called **SendUserContacts** which implements **UserContactsInterface** and performs an operation to get users in the **getUserContacts** function: 269 | 270 | ```php 271 | "App\Repository\SendUserContacts" 294 | ``` 295 | 296 | ## 7) Tech Stack 297 | 298 | **Laravel**, **Tailwind**, **Bouncer**, **Vue.js**, **Inertia.js** 299 | 300 | ## 8) Contributors 301 | 302 | - Cyrus Passi ([@Cyrus2505](https://twitter.com/Cyrus2505?s=20)) 303 | - Ila Sahu ([@ilasahu94](https://twitter.com/ilasahu94?lang=en)) 304 | - Sanket Sahu ([@sanketsahu](https://twitter.com/sanketsahu)) 305 | - Gaurav Guha ([@greedy_reader](https://twitter.com/greedy_reader?lang=en)) 306 | - Aditya Jamuar ([@GeekJamuar](https://twitter.com/geekjamuar?lang=en)) 307 | - Madhav Bansal ([@madhavb23](https://twitter.com/madhavb23?lang=en)) 308 | 309 | ## 9) How to Contribute 310 | 311 | Thank you for your interest in contributing to Sharedo! We are lucky to have you 🙂 Head over to [Contribution Guidelines](https://github.com/GeekyAnts/laravel-inertia-sharedo/blob/master/CONTRIBUTING.md) and learn how you can be a part of a wonderful, growing community. 312 | 313 | For major changes, please open an issue first to discuss changes and update tests as appropriate. 314 | 315 | ## 10) License 316 | 317 | Licensed under the MIT License. Please see the [License File](https://github.com/GeekyAnts/laravel-inertia-sharedo/blob/master/LICENSE.md) for more information. 318 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "geekyants/sharedo", 3 | "description": "Share Popup in Laravel Apps", 4 | "keywords": [ 5 | "share", 6 | "inertiajs", 7 | "vue", 8 | "laravel", 9 | "geekyants" 10 | ], 11 | "homepage": "https://github.com/GeekyAnts/laravel-inertia-sharedo", 12 | "license": "MIT", 13 | "type": "library", 14 | "authors": [ 15 | { 16 | "name": "Cyrus Passi", 17 | "email": "cyruspassi@gmail.com", 18 | "role": "Developer" 19 | } 20 | ], 21 | "require": { 22 | "php": ">=5.3.0", 23 | "laravel/ui": "^3.2" 24 | }, 25 | "require-dev": { 26 | "orchestra/testbench": "^4.0" 27 | }, 28 | "autoload": { 29 | "psr-4": { 30 | "Geekyants\\Sharedo\\": "src/" 31 | } 32 | }, 33 | "autoload-dev": { 34 | "psr-4": { 35 | "Geekyants\\Sharedo\\Tests\\": "tests" 36 | } 37 | }, 38 | "scripts": { 39 | "test": "vendor/bin/phpunit", 40 | "test-coverage": "vendor/bin/phpunit --coverage-html coverage" 41 | }, 42 | "config": { 43 | "sort-packages": true 44 | }, 45 | "extra": { 46 | "laravel": { 47 | "providers": [ 48 | "Geekyants\\Sharedo\\SharedoServiceProvider" 49 | ], 50 | "aliases": { 51 | "Sharedo": "Geekyants\\Sharedo\\SharedoFacade" 52 | } 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /public/images/sharedo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeekyAnts/laravel-inertia-sharedo/d4439af2dd5be40812e77e84811ed7daba8f53d8/public/images/sharedo.gif -------------------------------------------------------------------------------- /src/Controllers/SharedoController.php: -------------------------------------------------------------------------------- 1 | json(['searchUsers' => []]); 30 | $className = config('sharedo.typehead'); 31 | $controller = new $className; 32 | $users = json_decode($controller->getUserContacts($query)); 33 | return response()->json(['searchUsers' => $users]); 34 | } 35 | 36 | public function showSharedo($entity, $entityId) 37 | { 38 | 39 | try { 40 | $entityCapitalize = ucfirst($entity); 41 | $entityModel = substr($entityCapitalize, 0, -1); 42 | $entityModelSmall = substr($entity, 0, -1); 43 | $modelClass = config('sharedo.modelPath') . $entityModel; 44 | 45 | if (class_exists($modelClass)) { 46 | 47 | $authUser = Auth::user(); 48 | //find model 49 | $model = $modelClass::with('user')->findOrFail($entityId); 50 | $model['entity_name'] = $entityModelSmall; 51 | 52 | //if auth user does not own model 53 | if ($model->user_id != $authUser->id && Bouncer::cannot('write', $model)) { 54 | return back()->withErrors("You are not authorized!"); 55 | } 56 | 57 | //else assign ability to auth user 58 | if ($authUser->id == $model->user_id) { 59 | Bouncer::allow($authUser)->toOwn($model); 60 | Bouncer::allow($authUser)->to('write', $model); 61 | Bouncer::allow($authUser)->to('read', $model); 62 | } 63 | $findUser = false; 64 | if (config('sharedo.typehead')) 65 | $findUser = true; 66 | 67 | //get invited users 68 | list($users, $usersAbiltites) = InvitedUsersService::getInvitedUsers($model); 69 | Inertia::setRootView('sharedo'); 70 | return Inertia::render('Pages/index', ['entity' => $model, 'users' => $users, 'findUser' => $findUser, 'usersAbilities' => $usersAbiltites, 'modelOwner' => $model->user]); 71 | } else { 72 | return back()->withErrors("Model does not exist"); 73 | } 74 | } catch (\Exception $e) { 75 | 76 | return back()->withErrors($e->getMessage()); 77 | } 78 | } 79 | 80 | public function assignAbility(Request $request) 81 | { 82 | 83 | 84 | try { 85 | $request->validate([ 86 | 'emails' => 'required', 87 | 'emails.*.email' => 'bail|required|email|min:5', 88 | 'ability' => 'required' 89 | ]); 90 | 91 | $entityClass = ucfirst($request->entity_name); 92 | $modelClass = config('sharedo.modelPath') . $entityClass; 93 | $model = $modelClass::findOrFail($request->entity_id); 94 | $entityModelSmall = $request->entity_name . 's'; 95 | 96 | //get emails from request 97 | $emails = $request->emails; 98 | 99 | //assign abilities 100 | $message = AssignAbilityService::assignAbilities($emails, $request->ability, $model, $modelClass, $entityClass); 101 | 102 | return redirect()->route('sharedo', ['entity' => $entityModelSmall, 'entityId' => $request->entity_id])->with('success', $message); 103 | } catch (\Exception $e) { 104 | return back()->withErrors($e->getMessage()); 105 | } 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /src/Events/UserAbilityChanged.php: -------------------------------------------------------------------------------- 1 | user = $user; 29 | $this->ability = $ability; 30 | $this->entity = $entityClass; 31 | $this->model = $model; 32 | } 33 | 34 | /** 35 | * Get the channels the event should broadcast on. 36 | * 37 | * @return \Illuminate\Broadcasting\Channel|array 38 | */ 39 | public function broadcastOn() 40 | { 41 | return new PrivateChannel('channel-name'); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/Interfaces/UserContactsInterface.php: -------------------------------------------------------------------------------- 1 | user; 32 | $user->notify(new UserInvitedNotificaton($event->ability, $event->entity, $event->model)); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/Middleware/RestrictEntities.php: -------------------------------------------------------------------------------- 1 | route()->parameters()['entity']; 22 | if (!in_array($entity, config('sharedo.restrict-entities'))) { 23 | return back()->withErrors("Entity Restricted!"); 24 | } 25 | return $next($request); 26 | } 27 | 28 | return $next($request); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/Middleware/ShareInertiaData.php: -------------------------------------------------------------------------------- 1 | function () use ($request) { 17 | return [ 18 | 'success' => $request->session()->get('success'), 19 | 'error' => $request->session()->get('error'), 20 | ]; 21 | }, 22 | 23 | 24 | ]); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Notifications/UserInvitedNotificaton.php: -------------------------------------------------------------------------------- 1 | ability = $ability; 25 | $this->model = $model; 26 | $this->entity = $entity; 27 | } 28 | 29 | /** 30 | * Get the notification's delivery channels. 31 | * 32 | * @param mixed $notifiable 33 | * @return array 34 | */ 35 | public function via($notifiable) 36 | { 37 | return ['mail']; 38 | } 39 | 40 | /** 41 | * Get the mail representation of the notification. 42 | * 43 | * @param mixed $notifiable 44 | * @return \Illuminate\Notifications\Messages\MailMessage 45 | */ 46 | public function toMail($notifiable) 47 | { 48 | $user = $notifiable; 49 | $ability = $this->ability; 50 | $model = $this->model; 51 | $entity = $this->entity; 52 | return (new MailMessage)->markdown('sharedo::mail.invite-user', ['user' => $user, 'ability' => $ability, 'model' => $model, 'entity' => $entity]); 53 | } 54 | 55 | /** 56 | * Get the array representation of the notification. 57 | * 58 | * @param mixed $notifiable 59 | * @return array 60 | */ 61 | public function toArray($notifiable) 62 | { 63 | return [ 64 | // 65 | ]; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/Services/AssignAbilityService.php: -------------------------------------------------------------------------------- 1 | first(); 23 | if (!$user) { 24 | $user = self::createUser($email['email']); 25 | DB::table('new_users_sharedo')->insert([ 26 | 'user_id' => $user->id, 27 | 'has_ever_logged_in' => "false" 28 | ]); 29 | $newUser = true; 30 | $message = "Users Invited!"; 31 | } 32 | $validAbilities = ['read', 'write']; 33 | if (in_array($ability, $validAbilities)) { 34 | RemovePreviousAbiltiesService::removeAbilties($user, $model, $modelClass); 35 | Bouncer::allow($user)->to($ability, $model); 36 | $message = $newUser ? "Users Invited!" : "User Access Changed Successfully!"; 37 | } 38 | if ($ability == "remove") { 39 | RemovePreviousAbiltiesService::removeAbilties($user, $model, $modelClass); 40 | $message = "User Removed!"; 41 | } 42 | event(new UserAbilityChanged($user, $ability, $entityClass, $model)); 43 | } 44 | return $message; 45 | } 46 | 47 | public static function createUser($email) 48 | { 49 | 50 | $user = User::create([ 51 | 'name' => "User", 52 | 'email' => $email, 53 | 'password' => Hash::make('user'), 54 | ]); 55 | 56 | return $user; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/Services/InvitedUsersService.php: -------------------------------------------------------------------------------- 1 | select('abilities.name', 'permissions.entity_id') 17 | ->join('permissions', function ($join) use ($model) { 18 | $join->on('abilities.id', '=', 'permissions.ability_id') 19 | ->where('abilities.entity_id', $model->id); 20 | }) 21 | ->get(); 22 | 23 | return self::getUsersInformation($users, $model); 24 | } 25 | public static function getUsersInformation($users, $model) 26 | { 27 | 28 | 29 | //find user according to id and assign permission to user object 30 | 31 | $invitedUsers = []; 32 | $mapArray = array(); 33 | $userAbilities = array(); 34 | foreach ($users as $index => $key) { 35 | 36 | 37 | if ($key->entity_id == $model->user->id) 38 | continue; 39 | 40 | $key->name = ($key->name == "read") ? "Can View" : "Can Edit"; 41 | $abilityObject = new stdClass; 42 | if ($key->name == "Can View") { 43 | $abilityObject->value = "read"; 44 | $abilityObject->ability = "Can View"; 45 | } else { 46 | $abilityObject->value = "write"; 47 | $abilityObject->ability = "Can Edit"; 48 | } 49 | $userAbilities[$key->entity_id] = $abilityObject; 50 | $mapArray[$key->entity_id] = $key->name; 51 | array_push($invitedUsers, $key->entity_id); 52 | } 53 | $users = User::find($invitedUsers); 54 | foreach ($users as $user) { 55 | $user->ability = $mapArray[$user->id]; 56 | } 57 | return array($users, $userAbilities); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/Services/RemovePreviousAbiltiesService.php: -------------------------------------------------------------------------------- 1 | getAbilities(); 20 | foreach ($abilities as $ability) { 21 | if ($ability->entity_id = $model->id && $ability->entity_type == $model_name) { 22 | Bouncer::disallow($user)->to($ability->name, $model); 23 | } 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/SharedoPreset.php: -------------------------------------------------------------------------------- 1 | "^7.8.3", 24 | "@inertiajs/inertia" => "^0.8.3", 25 | "@inertiajs/inertia-vue" => "^0.5.4", 26 | "vue-multiselect" => "^2.1.6", 27 | "vue-template-compiler" => "^2.6.10", 28 | "vue" => "^2.5.17", 29 | "vue-loader" => "^15.9.6", 30 | "vue-multiselect" => "^2.1.6" 31 | ], $packages); 32 | } 33 | 34 | protected static function updateComposerArray(array $packages) 35 | { 36 | return array_merge([ 37 | "inertiajs/inertia-laravel" => "^0.3.5" 38 | ], $packages); 39 | } 40 | 41 | 42 | 43 | 44 | protected static function scaffoldComponents() 45 | { 46 | copy(__DIR__ . '/resources/js/sharedo.js', resource_path('js/sharedo.js')); 47 | copy(__DIR__ . '/resources/views/sharedo.blade.php', resource_path('views/sharedo.blade.php')); 48 | tap(new Filesystem, function ($fs) { 49 | $fs->copyDirectory(__DIR__ . '/resources/js/Sharedo', resource_path('js/Sharedo')); 50 | }); 51 | } 52 | 53 | protected static function updateComposer($dev = true) 54 | { 55 | if (!file_exists(base_path('composer.json'))) { 56 | return; 57 | } 58 | 59 | $configurationKey = $dev ? 'require-dev' : 'require'; 60 | 61 | $packages = json_decode(file_get_contents(base_path('composer.json')), true); 62 | 63 | $packages[$configurationKey] = static::updateComposerArray( 64 | array_key_exists($configurationKey, $packages) ? $packages[$configurationKey] : [] 65 | ); 66 | 67 | ksort($packages[$configurationKey]); 68 | 69 | file_put_contents( 70 | base_path('composer.json'), 71 | json_encode($packages, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT) . PHP_EOL 72 | ); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/SharedoServiceProvider.php: -------------------------------------------------------------------------------- 1 | info('Sharedo scaffolding installed successfully.'); 27 | $command->info('Please run "composer update, npm install && npm run dev" to compile your fresh scaffolding.'); 28 | }); 29 | 30 | 31 | 32 | $this->bootInertia(); 33 | 34 | 35 | 36 | 37 | $this->loadViewsFrom(__DIR__ . '/./resources/views', 'sharedo'); 38 | $this->loadMigrationsFrom(__DIR__ . '/./migrations'); 39 | $this->loadRoutesFrom(__DIR__ . '/./routes/web.php'); 40 | app('router')->aliasMiddleware('restrict-entities', RestrictEntities::class); 41 | 42 | 43 | if ($this->app->runningInConsole()) { 44 | $this->publishes([ 45 | __DIR__ . '/./config/config.php' => config_path('sharedo.php'), 46 | ], 'config'); 47 | 48 | // Publishing the views. 49 | $this->publishes([ 50 | __DIR__ . '/./resources/views/mail' => resource_path('views/vendor/sharedo/mail'), 51 | ], 'mail'); 52 | 53 | 54 | 55 | // Registering package commands. 56 | 57 | } 58 | } 59 | 60 | /** 61 | * Register the application services. 62 | */ 63 | public function register() 64 | { 65 | 66 | 67 | 68 | // Automatically apply the package configuration 69 | $this->mergeConfigFrom(__DIR__ . '/./config/config.php', 'sharedo'); 70 | 71 | 72 | 73 | 74 | // Register the main class to use with the facade 75 | $this->app->singleton('sharedo', function () { 76 | return new Sharedo; 77 | }); 78 | } 79 | protected function bootInertia() 80 | { 81 | 82 | $router = $this->app['router']; 83 | $router->pushMiddlewareToGroup('web', ShareInertiaData::class); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /src/config/config.php: -------------------------------------------------------------------------------- 1 | "App\Models\\", 8 | 'middleware' => ['web', 'auth'], 9 | 'restrict-entities' => [], 10 | "typehead" => "", 11 | ]; 12 | -------------------------------------------------------------------------------- /src/migrations/2021_02_18_072401_create_new_users_sharedo_table.php: -------------------------------------------------------------------------------- 1 | id(); 18 | $table->string('has_ever_logged_in'); 19 | $table->unsignedBigInteger('user_id'); 20 | $table 21 | ->foreign('user_id') 22 | ->references('id') 23 | ->on('users'); 24 | $table->timestamps(); 25 | }); 26 | } 27 | 28 | /** 29 | * Reverse the migrations. 30 | * 31 | * @return void 32 | */ 33 | public function down() 34 | { 35 | Schema::dropIfExists('new_users_sharedo'); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/resources/js/Sharedo/Pages/Access.vue: -------------------------------------------------------------------------------- 1 | 14 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /src/resources/js/Sharedo/Pages/FlashMessage.vue: -------------------------------------------------------------------------------- 1 | 58 | 59 | -------------------------------------------------------------------------------- /src/resources/js/Sharedo/Pages/Layout.vue: -------------------------------------------------------------------------------- 1 | 6 | 7 | 22 | -------------------------------------------------------------------------------- /src/resources/js/Sharedo/Pages/Owner.vue: -------------------------------------------------------------------------------- 1 | 23 | 30 | 35 | 36 | -------------------------------------------------------------------------------- /src/resources/js/Sharedo/Pages/Tag.vue: -------------------------------------------------------------------------------- 1 | 60 | 216 | 217 | 302 | 303 | -------------------------------------------------------------------------------- /src/resources/js/Sharedo/Pages/Users.vue: -------------------------------------------------------------------------------- 1 | 46 | 102 | 103 | 193 | -------------------------------------------------------------------------------- /src/resources/js/Sharedo/Pages/index.vue: -------------------------------------------------------------------------------- 1 | 149 | 237 | 248 | -------------------------------------------------------------------------------- /src/resources/js/sharedo.js: -------------------------------------------------------------------------------- 1 | const app = new Vue({ 2 | el: "#app", 3 | }); 4 | 5 | import { App, plugin } from "@inertiajs/inertia-vue"; 6 | import Vue from "vue"; 7 | Vue.use(plugin); 8 | 9 | const el = document.getElementById("app"); 10 | 11 | new Vue({ 12 | render: (h) => 13 | h(App, { 14 | props: { 15 | initialPage: JSON.parse(el.dataset.page), 16 | resolveComponent: (name) => 17 | require(`./Sharedo/${name}`).default, 18 | }, 19 | }), 20 | }).$mount(el); 21 | -------------------------------------------------------------------------------- /src/resources/views/mail/invite-user.blade.php: -------------------------------------------------------------------------------- 1 | @component('mail::message') 2 | # Hi {{$user->name}} 3 | 4 | 5 | @if($ability=="read") 6 | You have been granted Read access for {{$entity}} with id {{$model->id}} 7 | @elseif($ability=="write") 8 | You have been granted Write access for {{$entity}} with id {{$model->id}} 9 | @elseif($ability=="remove") 10 | Access revoked from {{$entity}} with id {{$model->id}} 11 | @endif 12 | 13 | Thanks,
14 | {{ config('app.name') }} 15 | @endcomponent -------------------------------------------------------------------------------- /src/resources/views/sharedo.blade.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | @inertia 15 | 16 | 17 | -------------------------------------------------------------------------------- /src/routes/web.php: -------------------------------------------------------------------------------- 1 | config('sharedo.middleware')], function () { 8 | Route::get('sharedo/searchUsers/{query}', [SharedoController::class, 'searchUsers']); 9 | Route::get('/sharedo/{entity}/{entityId}', [SharedoController::class, 'showSharedo'])->middleware('restrict-entities')->name('sharedo'); 10 | Route::post('/sharedo', [SharedoController::class, 'assignAbility']); 11 | }); 12 | --------------------------------------------------------------------------------