<?php

namespace Student\Student\Policies;

use App\User;
use Illuminate\Auth\Access\HandlesAuthorization;
use Student\Student\Models\Key;

class KeyPolicy
{
    use HandlesAuthorization;

    /**
     * Determine if the given user can view the key.
     *
     * @param User $user
     * @param Key $key
     *
     * @return bool
     */
    public function view(User $user, Key $key)
    {
        if ($user->canDo('student.key.view') && $user->is('admin')) {
            return true;
        }

        return $user->id === $key->user_id;
    }

    /**
     * Determine if the given user can create a key.
     *
     * @param User $user
     * @param Key $key
     *
     * @return bool
     */
    public function create(User $user)
    {
        return  $user->canDo('student.key.create');
    }

    /**
     * Determine if the given user can update the given key.
     *
     * @param User $user
     * @param Key $key
     *
     * @return bool
     */
    public function update(User $user, Key $key)
    {
        if ($user->canDo('student.key.update') && $user->is('admin')) {
            return true;
        }

        return $user->id === $key->user_id;
    }

    /**
     * Determine if the given user can delete the given key.
     *
     * @param User $user
     * @param Key $key
     *
     * @return bool
     */
    public function delete(User $user, Key $key)
    {
        if ($user->canDo('student.key.delete') && $user->is('admin')) {
            return true;
        }

        return $user->id === $key->user_id;
    }

    /**
     * Determine if the user can perform a given action ve.
     *
     * @param [type] $user    [description]
     * @param [type] $ability [description]
     *
     * @return [type] [description]
     */
    public function before($user, $ability)
    {
        if ($user->isSuperUser()) {
            return true;
        }
    }
}