laravel use SSH in controller - laravel

Im trying to use SSH:: command in my controller but I'm facing error:
Class 'SSH' not found
my controller:
namespace App\Http\Controllers;
use Session;
use DB;
use SSH;
and call action:
SSH::run(
array(
'cd /var/www/laravel',
'node accept.js '
)
);
so where the problem could be?

You need to follow the installation guide of Laravel SSH package:
1. Install it via Composer:
Add this to your composer.json:
"require": {
"laravelcollective/remote": "~5.0"
}
Then run composer update.
2. Add new provider to the providers array of config/app.php:
'providers' => [
// ...
'Collective\Remote\RemoteServiceProvider',
// ...
],
3. Add two class aliases to the aliases array of config/app.php:
'aliases' => [
// ...
'SSH' => 'Collective\Remote\RemoteFacade',
// ...
],

Related

Upload to Dropbox not working from Laravel with Dropbox driver

I'm trying to upload a (DomPDF generated) PDF file to Dropbox with the Dropbox driver in Laravel 8. I've installed spatie/flysystem-dropbox and created a DropboxServiceProvider.php with following contents:
<?php
namespace App\Providers;
use Storage;
use Illuminate\Support\ServiceProvider;
use League\Flysystem\Filesystem;
use Spatie\Dropbox\Client;
use Spatie\FlysystemDropbox\DropboxAdapter;
class DropboxServiceProvider extends ServiceProvider
{
public function boot()
{
Storage::extend('dropbox', function ($app, $config) {
$client = new Client([$config['key'], $config['secret']]);
return new Filesystem(new DropboxAdapter($client));
});
}
public function register()
{
//
}
}
The service provider is also added to my config/app providers:
'providers' => [
...
App\Providers\DropboxServiceProvider::class,
...
]
In my config/filesystems I've added the dropbox driver (dropbox app key and secret are also set in .env file):
'dropbox' => [
'driver' => 'dropbox',
'key' => env('DROPBOX_APP_KEY'),
'secret' => env('DROPBOX_APP_SECRET'),
]
Now, when I try to run the following code, it returns false and the file doesn't appear in my Dropbox. When I change the disk to 'local', the file gets uploaded to my local storage.
$path = "pdf/file.pdf";
$storage_path = Storage::path($path);
$contents = file_get_contents($storage_path);
$upload = Storage::disk('dropbox')->put($path, $contents);
return $upload;
I've already tried clearing my config by running php artisan config:clear. After trying many different things, I have no idea what I'm doing wrong, so any advice will be appreciated!
The problem was not in the code, but in the permissions in my dropbox app: files.content.write wasn't enabled yet.

Call to undefined function App\Http\Controllers\Datatables()

I already install yajra package but it still show me error
my controller and i also used namespace
use Datatables;
return Datatables()->of($data)->make(true);
my providers
Yajra\Datatables\DatatablesServiceProvider::class,
'aliases'
'Datatables'=>Yajra\Datatables\DatatablesServiceProvider::class,
You have to call it with a helper and you don't have to include it. You accessed the class of datatables, not the helper. Do it like this.
return datatables()->of($data)->make(true);
Yajra namespace is Yajra\DataTables. so you need to call it as below.
use Yajra\DataTables\Datatables;
or if you want to call only use Datatables then you need to set providers and alias.
config/app.php
.....
'providers' => [
....
Yajra\DataTables\DataTablesServiceProvider::class,
]
'aliases' => [
....
'DataTables' => Yajra\DataTables\Facades\DataTables::class,
]
.....
And then you can use simply
use DataTables;

Is there an tutorial for how to set up an ldap server and after to connect it with a simple laravel app?

I set up the ldap using DigitalOcean tutorial and installed AdLdap2 package and if anybody can clarify to me why on login to the ldap server still try to login with default username and password, i can't explain properly the problem if it doesn't make sense to you, can you just put here some links which helped you to set up the server and connect a laravel project with ldap
I know I'm a little late but these are my notes from when I set it up for one of my apps.
Adldap2-Laravel (NoDatabaseProvider)
Installation
Install the Adldap2-laravel package:
$ composer require adldap2/adldap2-laravel
Create the auth scaffolding:
$ php artisan make:auth
Configuration
Define the following environment variables in your .env file:
# .env
LDAP_HOSTS=[192.168.1.10]
LDAP_PORT=[389]
LDAP_BASE_DN=[DC=contoso,DC=com]
LDAP_USERNAME=[ldap#contoso.com]
LDAP_PASSWORD=[password123]
Disable the default routes if only using LDAP authentication. In your /routes/web.php file, make the following changes for Auth::routes() :
# /routes/web.php
Auth::routes([
'reset' => false,
'verify' => false,
'register' => false,
])
In your /resources/views/auth/login.blade.php file, change the label for email address to Username, remove the #error('email) section from the corresponding text input and change the name on that text input to whatever LDAP attribute you are looking users up by (samaccountname or userprincipalname) Finished text input should look something similar to this:
# /resources/views/auth/login.blade.php
<input type="text" class="form-control" name="samaccountname" value="{{ old('samaccountname') }}" required autofocus>
Create the ldap.php and ldap_auth.php file by running the following two commands:
$ php artisan vendor:publish --provider "Adldap\Laravel\AdldapServiceProvider"
$ php artisan vendor:publish --provider "Adldap\Laravel\AdldapAuthServiceProvider"
Change the user driver to use ldap instead of eloquent under providers in /config/auth.php and comment out the model below it, like so:
# /config/auth.php
'providers' => [
'users' => [
'driver' => 'ldap', // <- was eloquent, changed to ldap
# 'model' => App\User::class, // <- commented out, not using anymore
In /config/ldap_auth.php:
Change the provider from DatabaseUserProvider to NoDatabaseUserProvider:
# /config/ldap_auth.php
'provider' => Adldap\Laravel\Auth\NoDatabaseProvider::class
Change the locate_users_by under ldap to whatever field you plan to authenticate users by (samaccountname or userprincipalname)
# /config/ldap_auth.php
'ldap' => [
'locate_users_by' => 'samaccountname',
'bind_users_by' => 'distinguishedname',
],
In your /app/Http/Controllers/Auth/LoginController.php file, you must add a username() function that returns the LDAP attribute you are authenticating users by:
# /app/Http/Controllers/Auth/LoginController.php
public function username()
{
return 'samaccountname';
}
In your /config/app.php file, it's helpful to add the alias for the Adldap Facade to keep from having to type it out every time you need to access it:
# /config/app.php
'aliases' => [
'App' => Illuminate\Support\Facades\App::class,
'Arr' => Illuminate\Support\Arr::class,
. // scroll
. // to
. // the
. // bottom
'View' => Illuminate\Support\Facades\View::class,
'Adldap' => Adldap\Laravel\Facades\Adldap::class // <- add this
],
Usage
After successfully authenticating, if you add use Auth; in your controller with the other use contracts in the beginning of the file, you can access the authenticated user's attributes like so:
# /app/Http/Controllers/HomeController
use Auth;
public function index()
{
$user = Auth::user();
$cn = $user->getCommonName(); // assigns currently authenticated user's Common Name
return view('home')->with('cn', $cn);
// using {{ $cn }} on the home's view would output the Common Name
}
If you want to query the LDAP server for all other users, you can add use Adldap; with the other use contracts and use the query builder:
# /app/Http/Controllers/HomeController
use Adldap;
public function index()
{
$users = Adldap::search()->where('objectclass', '=', 'person')->sortBy('cn', 'asc')->get();
return view('home', compact('users'))
// use a foreach loop on the view to iterate through users and attributes
}

Lumen Cache\Store is not instantiable

I'm quite new to Laravel and Lumen, so my question may be a little simple, but I couldn't find any useful answer yet.
Lumen version is 5.1.
So I tried to create a data repository supported by a cache. Firstly I want to use FileStore, then I want to move to some more appropriate.
I tried to inject Cache repository like this:
<?php
namespace App\Repositories;
use Illuminate\Cache\Repository;
class DataRepository
{
private $cache;
public function __construct(Repository $cache)
{
$this->cache = $cache;
}
}
It seemed pretty simple to me. But when I try to use this repo in my controller, and tried to inject this repo into it, during instantiation I get the following error:
BindingResolutionException in Container.php line 749:
Target [Illuminate\Contracts\Cache\Store] is not instantiable.
I guessed the repository cannot find the matching and useable store implementation. When I tried to bind the Store to \Illumante\Cache\FileStore like this:
$this->app->bind(\Illuminate\Contracts\Cache\Store::class, \Illuminate\Cache\FileStore::class);
I got a new kind of error:
Unresolvable dependency resolving [Parameter #1 [ <required> $directory ]] in class Illuminate\Cache\FileStore
I guess I have a more complicated config issue, so I didn't want to walk through the dependency tree.
In my .env I have these:
CACHE_DRIVER=file and SESSION_DRIVER=file
In Lumen I explicitly enabled the facades, the DotEnv (and the eloquent also for my data repositories).
Dotenv::load(__DIR__.'/../');
$app = new Laravel\Lumen\Application(
realpath(__DIR__.'/../')
);
$app->withFacades();
$app->withEloquent();
I tried to add a cache.php configuration. In the bootstrap/app.php I added $app->configure('cache'); to use it with the following configs:
<?php
return [
'default' => env('CACHE_DRIVER', 'file'),
'stores' => [
'file' => [
'driver' => 'file',
'path' => storage_path('framework/cache'),
],
],
];
Could you help me, how can I bootstrap the Cache properly?
Answer
The cache implementation in Lumen is registered as:
Illuminate\Contracts\Cache\Repository
Not
Illuminate\Cache\Repository
So you may change your code to:
<?php
namespace App\Repositories;
use Illuminate\Contracts\Cache\Repository;
class DataRepository
{
private $cache;
public function __construct(Repository $cache)
{
$this->cache = $cache;
}
}
P.S You don't need to configure cache, since Lumen will configure any cache configuration automatically.
Tricks
But if you still want to use Illuminate\Cache\Repository, you may bind it first in your ServiceProvider or bootstrap/app.php file:
use Illuminate\Cache\Repository as CacheImplementation;
use Illuminate\Contracts\Cache\Repository as CacheContract;
$app->singleton(CacheImplementation::class, CacheContract::class);

Access to composer autoloaded files in laravel 5

Trying to use a non-Laravel package: https://packagist.org/packages/luceos/on-app
Edited composer.json to require it and did the composer install, update, then dump-autoload -o.
This package requires an initialization: vendor/luceos/on-app/src/OnAppInit.php
Which isn't a class and only has the one method. But it doesn't seem to be loaded when I try to bind it in a service provider. The version for the cloud is initiated in the OnAppInit.php but that isn't being done so the "version isn't supported" error comes up of course.
I know that I am missing a small detail but can't find it. Maybe in the service provider??
composer.json
"require": {
"luceos/on-app": "~3.5"
"autoload": {
"psr-4": {
"Luceos\\OnApp\\": "vendor/luceos/on-app/src/"
config/app.php
'providers' => [
'App\Providers\OnAppServiceProvider',
app/Providers/OnAppServiceProvider.php
public function register()
{
$this->app->bind('onapp', function($app)
{
$hostname = 'http://cloud';
$username = 'email#foo.com';
$password = 'api_key';
$factory = new \OnApp_Factory($hostname, $username, $password);
$setting = $factory->factory('Settings')->getList();
return $setting;
});
}
Looks like its there...
vendor/composer/autoload_files.php
$vendorDir . '/luceos/on-app/src/OnAppInit.php',
vendor/composer/autoload_psr4.php
'Luceos\\OnApp\\' => array($vendorDir . '/luceos/on-app/src'),
Regarding the Guzzle question:
Just include it in your composer.json file:
"guzzlehttp/guzzle": "~5.0"
And then just use the normal
$client = new GuzzleHttp\Client();
Just don't forget to to composer dump-autoload

Resources