Skip to content

Commit

Permalink
Add readme and minor tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeroen-G committed Aug 16, 2017
1 parent 8b33478 commit 2f823bf
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 58 deletions.
112 changes: 67 additions & 45 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,83 +1,105 @@
# :package_name
# GuestPass for Laravel

[![Latest Version on Packagist][ico-version]][link-packagist]
[![Software License][ico-license]](LICENSE.md)
[![Build Status][ico-travis]][link-travis]
[![Coverage Status][ico-scrutinizer]][link-scrutinizer]
[![Quality Score][ico-code-quality]][link-code-quality]
[![Total Downloads][ico-downloads]][link-downloads]

**Note:** Replace ```:author_name``` ```:author_username``` ```:author_website``` ```:author_email``` ```:vendor``` ```:package_name``` ```:package_description``` with their correct values in [README.md](README.md), [CHANGELOG.md](CHANGELOG.md), [CONTRIBUTING.md](CONTRIBUTING.md), [LICENSE.md](LICENSE.md) and [composer.json](composer.json) files, then delete this line. You can run `$ php prefill.php` in the command line to make all replacements at once. Delete the file prefill.php as well.
## Installation

This is where your description should go. Try and limit it to a paragraph or two, and maybe throw in a mention of what
PSRs you support to avoid any confusion with users and contributors.
Via Composer

## Structure
``` bash
$ composer require jeroen-g/guestpass
```

If any of the following are applicable to your project, then the directory structure should follow industry best practises by being named the following.
If you run Laravel 5.5, the service provider and facade are autodiscovered. If not then add them manually:

```php
JeroenG\GuestPass\GuestPassServiceProvider::class,
// ...
'GuestPass' => JeroenG\GuestPass\GuestPassFacade::class,
```
bin/
config/
src/
tests/
vendor/

## Usage

To use Guest Passes, you will need two models: one that is the owner granting guest access (typically a user) and another that is the object to which access is being granted. In the examples below, a photo is used for this.

### Creating new Guest Passes

Requires the owner and object models.
```php
GuestPass::create($user, $photo);
```
Returns true if successfull, false otherwise.

### Retrieving Guest Pass data

## Install
A Guest Pass contains the following data: `owner_model`, `owner,id`; `object_model`, `object_id`; `key` (unique); `view` (nullable).

Via Composer
#### Getting all keys of the owner
Requires the owner model.
```php
GuestPass::getKeysOf($user);
```
Returns a collection of all Guest Pass keys and their corresponding data is attached as well.

``` bash
$ composer require :vendor/:package_name
#### Finding a Guest Pass for corresponding owner and object
Requires the owner and object model.
```php
GuestPass::findGuestPass($user, $photo);
```
Returns an Eloquent model (or throws an exception).

## Usage
#### Get one specific Guest Pass by its key
Requires the key (string).
```php
GuestPass::getGuestPass($key);
```
Returns an Eloquent model (or throws an exception).

``` php
$skeleton = new League\Skeleton();
echo $skeleton->echoPhrase('Hello, League!');
### Checking ownership
Requires the owner and Guest Pass models.
```php
GuestPass::isOwner($user, $guestpass);
```
Returns true or false.

## Change log
### Access controller

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
The package ships with a controller that checks for the `/gp/{owner id}/{key}` route and when valid it returns the view (404 otherwise). Each view is passed the object and the Guest Pass models.
The views will be sought in the `resources/views/guests/` directory.

## Testing
### Custom views

``` bash
$ composer test
When creating a Guest Pass it is possible to pass a custom view as the third parameter
```php
GuestPass::create($user, $photo, 'album');
```
In this case, the access controller will not use `photo.blade.php` (which would be the default) but `album.blade.php` but the directory remains the same and it is not necessary to add the file extension.

## Contributing
## Changelog

Please see [CONTRIBUTING](CONTRIBUTING.md) and [CONDUCT](CONDUCT.md) for details.
Please see [changelog.md](changelog.md) for what has changed recently.

## Security
## Contributing

If you discover any security related issues, please email :author_email instead of using the issue tracker.
Please see [contributing.md](contributing.md) for details.

## Credits

- [:author_name][link-author]
- [JeroenG][link-author]
- [All Contributors][link-contributors]

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
The EU Public License. Please see [license.md](license.md) for more information.

[ico-version]: https://img.shields.io/packagist/v/:vendor/:package_name.svg?style=flat-square
[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square
[ico-travis]: https://img.shields.io/travis/:vendor/:package_name/master.svg?style=flat-square
[ico-scrutinizer]: https://img.shields.io/scrutinizer/coverage/g/:vendor/:package_name.svg?style=flat-square
[ico-code-quality]: https://img.shields.io/scrutinizer/g/:vendor/:package_name.svg?style=flat-square
[ico-downloads]: https://img.shields.io/packagist/dt/:vendor/:package_name.svg?style=flat-square
[ico-version]: https://img.shields.io/packagist/v/Jeroen-G/GuestPass.svg?style=flat-square
[ico-travis]: https://img.shields.io/travis/Jeroen-G/GuestPass/master.svg?style=flat-square
[ico-code-quality]: https://img.shields.io/scrutinizer/g/Jeroen-G/GuestPass.svg?style=flat-square

[link-packagist]: https://packagist.org/packages/:vendor/:package_name
[link-travis]: https://travis-ci.org/:vendor/:package_name
[link-scrutinizer]: https://scrutinizer-ci.com/g/:vendor/:package_name/code-structure
[link-code-quality]: https://scrutinizer-ci.com/g/:vendor/:package_name
[link-downloads]: https://packagist.org/packages/:vendor/:package_name
[link-packagist]: https://packagist.org/packages/Jeroen-G/GuestPass
[link-travis]: https://travis-ci.org/Jeroen-G/GuestPass
[link-code-quality]: https://scrutinizer-ci.com/g/Jeroen-G/GuestPass
[link-author]: https://github.com/:author_username
[link-contributors]: ../../contributors
[link-contributors]: ../../contributors
2 changes: 1 addition & 1 deletion src/Controllers/Access.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __invoke($owner, $key)
}
// If there is a view handle saved, check if the file exists.
} elseif (file_exists(resource_path('views/guests/'.$pass->view.'.blade.php'))) {
return view('guests/'.$$pass->view, ['object' => $object]);
return view('guests/'.$$pass->view, ['object' => $object, 'guestpass' => $pass]);
}
// In case neither exists, abort.
abort(404);
Expand Down
7 changes: 6 additions & 1 deletion src/GuestPassService.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,17 @@ public function isOwner(Model $owner, Model $guestpass)
return $owner->id == $guestpass->owner_id;
}

/**
* Get all keys belonging to the owner.
* @param lluminate\Database\Eloquent\Model $owner
* @return array
*/
public function getKeysOf(Model $owner)
{
return GuestPass::where([
'owner_id' => $owner->id,
'owner_model' => get_class($owner),
])->get();
])->get()->keyBy('key');
}

/**
Expand Down
12 changes: 1 addition & 11 deletions tests/UnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,6 @@

class UnitTest extends BaseTestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testBasicTest()
{
$this->assertTrue(true);
}

public function test_create_guestpass()
{
$this->assertTrue(
Expand Down Expand Up @@ -46,7 +36,7 @@ public function test_get_keys()
$this->assertTrue(
$this->guestpass()->getKeysOf($user)->contains(function ($item) {
return $item->object_id == 92;
}));
}));
}

public function test_find()
Expand Down

0 comments on commit 2f823bf

Please sign in to comment.