Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support fileMode fileOwnership in create migration #18676

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
83fda4b
Merge pull request #1 from yiisoft/master
rhertogh Jul 30, 2015
5231894
Merge branch 'master' of https://github.com/yiisoft/yii2
rhertogh Jan 24, 2020
e388dfc
Merge branch 'master' of https://github.com/yiisoft/yii2
rhertogh Jul 16, 2020
6b56e2c
Merge branch 'master' of https://github.com/yiisoft/yii2
rhertogh Aug 20, 2020
1676da7
Merge branch 'master' of https://github.com/yiisoft/yii2 into master
rhertogh Oct 31, 2020
accf01e
Merge branch 'master' of https://github.com/yiisoft/yii2 into master
rhertogh May 23, 2021
191ad7d
Added support for `fileMode` and `fileOwnership` in BaseMigrateContro…
rhertogh May 23, 2021
0782abc
Updated changelog and documentation for `fileMode` and `fileOwnership…
rhertogh May 23, 2021
a700921
Merge branch 'master' of https://github.com/yiisoft/yii2 into support…
rhertogh May 23, 2021
6aac35f
Fixed format of changelog after merge
rhertogh May 23, 2021
6bb0b48
Changed names of to fileMode and fileOwnership to newFileMode and new…
rhertogh May 23, 2021
3610f8a
Removed incorrectly added newlines in db-migrations.md
rhertogh May 23, 2021
57dfd2e
Fixed function return type and don't change group if user change failed
rhertogh May 24, 2021
5967d5d
Fixed phpdoc bloks for `fileMode` and `fileOwnership` in BaseMigrateC…
rhertogh May 24, 2021
ec73d07
Update framework/CHANGELOG.md
rhertogh May 24, 2021
0d7ceed
Raising exception when failing to set `newFileMode` or `newFileOwners…
rhertogh May 24, 2021
e17986c
Merge branch 'master' of https://github.com/yiisoft/yii2 into support…
rhertogh May 24, 2021
cae6863
Added $mode to BaseFileHelper::changeOwnership() and raise errors whe…
rhertogh May 24, 2021
6ffeb57
Added unit test for FileHelper::changeOwnership()
rhertogh May 24, 2021
9c52020
Merge branch 'master' of https://github.com/yiisoft/yii2 into support…
rhertogh May 28, 2021
122c0d2
Moved setup code from changeOwnershipInvalidArgumentsProvider() to te…
rhertogh May 28, 2021
a574908
Skipping chown() tests in FileHelperTest::testChangeOwnership()
rhertogh May 28, 2021
0fc0745
Revert "Skipping chown() tests in FileHelperTest::testChangeOwnership()"
rhertogh May 28, 2021
da294d7
Skipping chown() and chgrp() tests in FileHelperTest::testChangeOwner…
rhertogh May 28, 2021
2e1ea0f
Require-dev posix extension in composer
rhertogh May 28, 2021
0a7ff6f
Revert "Require-dev posix extension in composer"
rhertogh May 28, 2021
df5083c
Marking FileHelperTest::testChangeOwnership() skipped when posix exte…
rhertogh May 28, 2021
37311c7
Marking FileHelperTest::testChangeOwnership() skipped when posix exte…
rhertogh May 28, 2021
4e4ddf6
Apply suggestions from code review
rhertogh May 29, 2021
bff926e
Apply suggestions from code review
rhertogh May 29, 2021
407b4a7
Merge branch 'master' into support_file_mode_and_owner_in_create_migr…
May 29, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/guide/db-migrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,22 @@ class m150101_185401_create_news_table extends Migration

A list of all available methods for defining the column types is available in the API documentation of [[yii\db\SchemaBuilderTrait]].

> Info: The generated file permissions and ownership will be determined by the current environment. This might lead to
inaccessible files. This could, for example, happen when the migration is created within a docker container
and the files are edited on the host. In this case the `newFileMode` and/or `newFileOwnership` of the MigrateController
can be changed. E.g. in the application config:
```php
<?php
return [
'controllerMap' => [
'migrate' => [
'class' => 'yii\console\controllers\MigrateController',
'newFileOwnership' => '1000:1000', # Default WSL user id
'newFileMode' => 0660,
],
],
];
```

## Generating Migrations <span id="generating-migrations"></span>

Expand Down
2 changes: 2 additions & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ Yii Framework 2 Change Log
- Enh #18628: Added strings "software", and "hardware" to `$specials` array in `yii\helpers\BaseInflector` (kjusupov)
- Enh #18653: Added method `yii\helpers\BaseHtml::getInputIdByName()` (WinterSilence)
- Enh #18669: Changed visibility of `yii\web\User::checkRedirectAcceptable()` to `public` (rhertogh)
- Enh #18676: Added method `yii\helpers\BaseFileHelper::changeOwnership()` and properties `newFileMode`/`newFileOwnership` in `yii\console\controllers\BaseMigrateController` (rhertogh)
- Bug #18678: Fix `yii\caching\DbCache` to use configured cache table name instead of the default one in case of MSSQL varbinary column type detection (aidanbek)


2.0.42.1 May 06, 2021
---------------------

Expand Down
16 changes: 16 additions & 0 deletions framework/console/controllers/BaseMigrateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,20 @@ abstract class BaseMigrateController extends Controller
* or a file path.
*/
public $templateFile;
/**
* @var int the permission to be set for newly generated migration files.
* This value will be used by PHP chmod() function. No umask will be applied.
* If not set, the permission will be determined by the current environment.
rhertogh marked this conversation as resolved.
Show resolved Hide resolved
* @since 2.0.43
*/
public $newFileMode;
/**
* @var string|int the user and/or group ownership to be set for newly generated migration files.
* If not set, the ownership will be determined by the current environment.
* @since 2.0.43
* @see FileHelper::changeOwnership()
rhertogh marked this conversation as resolved.
Show resolved Hide resolved
*/
public $newFileOwnership;
/**
* @var bool indicates whether the console output should be compacted.
* If this is set to true, the individual commands ran within the migration will not be output to the console.
Expand Down Expand Up @@ -663,6 +677,8 @@ public function actionCreate($name)
return ExitCode::IOERR;
}

FileHelper::changeOwnership($file, $this->newFileOwnership, $this->newFileMode);

$this->stdout("New migration created successfully.\n", Console::FG_GREEN);
}

Expand Down
80 changes: 80 additions & 0 deletions framework/helpers/BaseFileHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Yii;
use yii\base\ErrorException;
use yii\base\Exception;
use yii\base\InvalidArgumentException;
use yii\base\InvalidConfigException;

Expand Down Expand Up @@ -874,4 +875,83 @@ protected static function normalizeOptions(array $options)

return $options;
}

/**
* Changes the Unix user and/or group ownership of a file or directory, and optionally the mode.
* Note: This function will not work on remote files as the file to be examined must be accessible
* via the server's filesystem.
* Note: On Windows, this function fails silently when applied on a regular file.
* @param string $path the path to the file or directory.
rhertogh marked this conversation as resolved.
Show resolved Hide resolved
* @param string|array|int|null $ownership the user and/or group ownership for the file or directory.
* When $ownership is a string, the format is 'user:group' where both are optional. E.g.
* 'user' or 'user:' will only change the user,
* ':group' will only change the group,
* 'user:group' will change both.
* When $owners is an index array the format is [0 => user, 1 => group], e.g. `[$myUser, $myGroup]`.
* It is also possible to pass an associative array, e.g. ['user' => $myUser, 'group' => $myGroup].
* In case $owners is an integer it will be used as user id.
* If `null`, an empty array or an empty string is passed, the ownership will not be changed.
* @param int|null $mode the permission to be set for the file or directory.
* If `null` is passed, the mode will not be changed.
*
* @since 2.0.43
*/
public static function changeOwnership($path, $ownership, $mode = null)
{
if (!file_exists($path)) {
throw new InvalidArgumentException('Unable to change ownerhip, "' . $path . '" is not a file or directory.');
}

if (empty($ownership) && $ownership !== 0 && $mode === null) {
return;
}

$user = $group = null;
if (!empty($ownership) || $ownership === 0 || $ownership === '0') {
if (is_int($ownership)) {
$user = $ownership;
} elseif (is_string($ownership)) {
$ownerParts = explode(':', $ownership);
$user = $ownerParts[0];
if (count($ownerParts) > 1) {
$group = $ownerParts[1];
}
} elseif (is_array($ownership)) {
$ownershipIsIndexed = ArrayHelper::isIndexed($ownership);
$user = ArrayHelper::getValue($ownership, $ownershipIsIndexed ? 0 : 'user');
$group = ArrayHelper::getValue($ownership, $ownershipIsIndexed ? 1 : 'group');
} else {
throw new InvalidArgumentException('$ownership must be an integer, string, array, or null.');
}
}

if ($mode !== null) {
if (!is_int($mode)) {
throw new InvalidArgumentException('$mode must be an integer or null.');
}
if (!chmod($path, $mode)) {
throw new Exception('Unable to change mode of "' . $path . '" to "0' . decoct($mode) . '".');
}
}
if ($user !== null && $user !== '') {
if (is_numeric($user)) {
$user = (int) $user;
} elseif (!is_string($user)) {
throw new InvalidArgumentException('The user part of $ownership must be an integer, string, or null.');
}
if (!chown($path, $user)) {
throw new Exception('Unable to change user ownership of "' . $path . '" to "' . $user . '".');
}
}
if ($group !== null && $group !== '') {
if (is_numeric($group)) {
$group = (int) $group;
} elseif (!is_string($group)) {
throw new InvalidArgumentException('The group part of $ownership must be an integer, string or null.');
}
if (!chgrp($path, $group)) {
throw new Exception('Unable to change group ownership of "' . $path . '" to "' . $group . '".');
}
}
}
}
Loading