Skip to content

Commit

Permalink
Merge pull request #1176 from proditis/writeup-language-indication
Browse files Browse the repository at this point in the history
Writeup language indication
  • Loading branch information
proditis authored May 7, 2024
2 parents 2e83d46 + a7e6a89 commit 90aa927
Show file tree
Hide file tree
Showing 31 changed files with 1,413 additions and 108 deletions.
646 changes: 646 additions & 0 deletions backend/migrations/m240507_121449_create_language_table.php

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

use yii\db\Migration;

/**
* Handles adding columns to table `{{%writeup}}`.
*/
class m240507_121450_add_language_column_to_writeup_table extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$this->addColumn('{{%writeup}}', 'language_id', $this->string(8)->after('formatter')->notNull()->defaultValue('en'));
$this->addForeignKey('fk_language_id', 'writeup', 'language_id', 'language', 'id');

}

/**
* {@inheritdoc}
*/
public function safeDown()
{
$this->dropColumn('{{%writeup}}', 'language');
}
}
161 changes: 72 additions & 89 deletions backend/modules/activity/models/SpinHistorySearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,95 +14,78 @@ class SpinHistorySearch extends SpinHistory
public $target_name;
public $username;

/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['id', 'target_id', 'player_id'], 'integer'],
[['created_at', 'updated_at', 'target_name', 'username'], 'safe'],
];
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['id', 'target_id', 'player_id'], 'integer'],
[['created_at', 'updated_at', 'target_name', 'username'], 'safe'],
];
}

/**
* {@inheritdoc}
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}

/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params)
{
$query = SpinHistory::find()->joinWith(['target', 'player']);

// add conditions that should always apply here

$dataProvider = new ActiveDataProvider([
'query' => $query,
]);

$this->load($params);

if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}

/**
* {@inheritdoc}
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}

/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params)
{
$query=SpinHistory::find()->joinWith(['target', 'player']);

// add conditions that should always apply here

$dataProvider=new ActiveDataProvider([
'query' => $query,
]);

$this->load($params);

if(!$this->validate())
{
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}

// grid filtering conditions
$query->andFilterWhere([
'spin_history.id' => $this->id,
'spin_history.target_id' => $this->target_id,
'spin_history.player_id' => $this->player_id,
]);

$query->andFilterWhere(['like', 'spin_history.created_at', $this->created_at]);
$query->andFilterWhere(['like', 'spin_history.updated_at', $this->updated_at]);
$query->andFilterWhere(['like', 'player.username', $this->username]);
$query->andFilterWhere(['like', 'target.name', $this->target_name]);
$dataProvider->setSort([
'attributes' => array_merge(
$dataProvider->getSort()->attributes,
[
'username' => [
'asc' => ['player.username' => SORT_ASC],
'desc' => ['player.username' => SORT_DESC],
],
'target_name' => [
'asc' => ['target.name' => SORT_ASC],
'desc' => ['target.name' => SORT_DESC],
],
]
),
]);


$query->andFilterWhere(['like', 'target.fqdn', $this->target]);
$dataProvider->setSort([
'defaultOrder' => ['id'=>SORT_DESC],
'attributes' => array_merge(
$dataProvider->getSort()->attributes,
[
'target' => [
'asc' => ['target_id' => SORT_ASC],
'desc' => ['target_id' => SORT_DESC],
],
]
),
]);


return $dataProvider;
}
// grid filtering conditions
$query->andFilterWhere([
'spin_history.id' => $this->id,
'spin_history.target_id' => $this->target_id,
'spin_history.player_id' => $this->player_id,
]);

$query->andFilterWhere(['like', 'spin_history.created_at', $this->created_at]);
$query->andFilterWhere(['like', 'spin_history.updated_at', $this->updated_at]);
$query->andFilterWhere(['like', 'player.username', $this->username]);
$query->andFilterWhere(['like', 'target.name', $this->target_name]);
$dataProvider->setSort([
'attributes' => array_merge(
$dataProvider->getSort()->attributes,
[
'username' => [
'asc' => ['player.username' => SORT_ASC],
'desc' => ['player.username' => SORT_DESC],
],
'target_name' => [
'asc' => ['target.name' => SORT_ASC],
'desc' => ['target.name' => SORT_DESC],
],
]
),
]);

return $dataProvider;
}
}
20 changes: 18 additions & 2 deletions backend/modules/activity/models/Writeup.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* @property string|null $status
* @property resource|null $comment
* @property string|null $formatter
* @property string $language
* @property string|null $created_at
* @property string|null $updated_at
*
Expand All @@ -42,17 +43,19 @@ public static function tableName()
public function rules()
{
return [
[['player_id', 'target_id'], 'required'],
[['player_id', 'target_id','language_id'], 'required'],
[['player_id', 'target_id'], 'integer'],
[['approved'], 'boolean'],
[['content', 'status', 'comment','formatter'], 'string'],
[['content', 'status', 'comment','formatter','language_id'], 'string'],
['formatter','in','range'=>['text','markdown']],
['formatter','default','value'=>'text'],
['language_id','default','value'=>'en'],
['status','in','range'=>['PENDING','NEEDS FIXES','REJECTED','OK']],
[['created_at', 'updated_at'], 'safe'],
[['player_id', 'target_id'], 'unique', 'targetAttribute' => ['player_id', 'target_id']],
[['player_id'], 'exist', 'skipOnError' => true, 'targetClass' => Player::class, 'targetAttribute' => ['player_id' => 'id']],
[['target_id'], 'exist', 'skipOnError' => true, 'targetClass' => Target::class, 'targetAttribute' => ['target_id' => 'id']],
[['language_id'], 'exist', 'skipOnError' => true, 'targetClass' => \app\modules\settings\models\Language::class, 'targetAttribute' => ['language_id' => 'id']],
];
}

Expand Down Expand Up @@ -91,6 +94,9 @@ public function attributeLabels()
'approved' => 'Approved',
'status' => 'Status',
'comment' => 'Comment',
'formatter' => 'Formatter',
'language_id' => 'Language',
'lang' => 'Language',
'created_at' => 'Created At',
'updated_at' => 'Updated At',
];
Expand Down Expand Up @@ -126,6 +132,16 @@ public function getTarget()
return $this->hasOne(Target::class, ['id' => 'target_id']);
}

/**
* Gets query for [[Target]].
*
* @return \yii\db\ActiveQuery|Target
*/
public function getLanguage()
{
return $this->hasOne(\app\modules\settings\models\Language::class, ['id' => 'language_id']);
}

/**
* {@inheritdoc}
* @return WriteupQuery the active query used by this AR class.
Expand Down
13 changes: 10 additions & 3 deletions backend/modules/activity/models/WriteupSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class WriteupSearch extends Writeup
public $username;
public $fqdn;
public $ipoctet;
public $lang;

/**
* {@inheritdoc}
Expand All @@ -22,7 +23,7 @@ public function rules()
{
return [
[['player_id', 'target_id', 'approved'], 'integer'],
[['content', 'status', 'comment', 'created_at', 'updated_at', 'username', 'fqdn', 'ipoctet'], 'safe'],
[['content', 'status', 'comment', 'created_at', 'updated_at', 'username', 'fqdn', 'ipoctet','lang'], 'safe'],
];
}

Expand All @@ -44,7 +45,7 @@ public function scenarios()
*/
public function search($params)
{
$query = Writeup::find()->joinWith(['player', 'target']);
$query = Writeup::find()->joinWith(['player', 'target','language']);

// add conditions that should always apply here

Expand All @@ -71,8 +72,10 @@ public function search($params)
->andFilterWhere(['like', 'writeup.updated_at', $this->updated_at])
->andFilterWhere(['like', 'content', $this->content])
->andFilterWhere(['like', 'writeup.status', $this->status])
->andFilterWhere(['like', 'writeup.language', $this->language])
->andFilterWhere(['like', 'comment', $this->comment]);
$query->andFilterWhere(['like', 'player.username', $this->username]);
$query->andFilterWhere(['like', 'language.l', $this->lang]);
$query->andFilterWhere(['like', 'target.fqdn', $this->fqdn]);
$query->andFilterWhere(['like', 'INET_NTOA(target.ip)', $this->ipoctet]);

Expand All @@ -84,7 +87,11 @@ public function search($params)
'asc' => ['player.username' => SORT_ASC],
'desc' => ['player.username' => SORT_DESC],
],
'fqdn' => [
'lang' => [
'asc' => ['language.l' => SORT_ASC],
'desc' => ['language.l' => SORT_DESC],
],
'fqdn' => [
'asc' => ['target.fqdn' => SORT_ASC],
'desc' => ['target.fqdn' => SORT_DESC],
],
Expand Down
1 change: 1 addition & 0 deletions backend/modules/activity/views/writeup/_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

<?= $form->field($model, 'formatter')->dropDownList([ 'text' => 'TEXT', 'markdown' => 'Markdown' ], ['prompt' => '']) ?>

<?= $form->field($model, 'language_id')->dropDownList(ArrayHelper::map(\app\modules\settings\models\Language::find()->orderBy('l')->all(), 'id', 'l'))?>
<?= $form->field($model, 'content')->textArea(['rows'=>15]) ?>

<?= $form->field($model, 'approved')->checkbox() ?>
Expand Down
18 changes: 17 additions & 1 deletion backend/modules/activity/views/writeup/help/create.php
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
<p>Comming soon...</p>
Create a writeup for a platform target.

Writeups allow players to share their methodology when solving a target.

The form fields include:
* `Target ID`: The target this writeup belongs to
* `Player ID`: The username that the writeup will display as author
* `Formatter`: The formatter that will be used by the frontend to display this writeup (supported formatters `Markdown`, `Text`).
* `Language`: The language that this writeup is written (defaults to `English`)
* `Content`: The raw content of the writeup
* `Approved`: Whether a writeup is approved or not
* `Status`: Status of writeup
* `OK`: Writeup is published and its ok to be displayed
* `Pending`: The writeup is pending for review by a moderator
* `Rejected`: The writeup is rejected by a moderator
* `Needs Fixes`: The writeup needs fixes as suggested by a moderator. (This usually means there is also a comment left for the player to read)
* `Comment`: A comment that will be left for the author to read
18 changes: 17 additions & 1 deletion backend/modules/activity/views/writeup/help/index.php
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
<p>Comming soon...</p>
Manage the player contributed Writeups for your platform targets.

Writeups allow players to share their methodology when solving a target.

The displayed fields include:
* `ID`: The record id
* `Username`: The username of the writeup author
* `Fqdn`: The name and IP of the target this writeup belongs to
* `Content`: A small portion of the writeup content
* `Approved`: Whether a writeup is approved or not
* `Status`: Status of writeup
* `OK`: Writeup is published and its ok to be displayed
* `Pending`: The writeup is pending for review by a moderator
* `Rejected`: The writeup is rejected by a moderator
* `Needs Fixes`: The writeup needs fixes as suggested by a moderator. (This usually means there is also a comment left for the player to read)
* `Language`: The language that this writeup is written (defaults to `English`)
* `Created At/Updated At`: When was the writeup created and last updated at
18 changes: 17 additions & 1 deletion backend/modules/activity/views/writeup/help/update.php
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
<p>Comming soon...</p>
Update an existing writeup for a platform target.

Writeups allow players to share their methodology when solving a target.

The form fields include:
* `Target ID`: The target this writeup belongs to
* `Player ID`: The username that the writeup will display as author
* `Formatter`: The formatter that will be used by the frontend to display this writeup (supported formatters `Markdown`, `Text`).
* `Language`: The language that this writeup is written (defaults to `English`)
* `Content`: The raw content of the writeup
* `Approved`: Whether a writeup is approved or not
* `Status`: Status of writeup
* `OK`: Writeup is published and its ok to be displayed
* `Pending`: The writeup is pending for review by a moderator
* `Rejected`: The writeup is rejected by a moderator
* `Needs Fixes`: The writeup needs fixes as suggested by a moderator. (This usually means there is also a comment left for the player to read)
* `Comment`: A comment that will be left for the author to read
7 changes: 6 additions & 1 deletion backend/modules/activity/views/writeup/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@
'attribute'=>'status',
'filter'=>['OK'=>'OK','PENDING'=>'PENDING','REJECTED'=>'REJECTED','NEEDS FIXES'=>'NEEDS FIXES'],
],
//'comment',
[
'attribute'=>'lang',
'label'=>'Language',
'value'=>'language.l'

],
'created_at',
'updated_at',

Expand Down
4 changes: 4 additions & 0 deletions backend/modules/activity/views/writeup/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
'target.name',
'target.ipoctet',
'formatter',
[
'attribute'=>'language.l',
'label'=>'Language'
],
[
'attribute'=>'content',
'format'=>'raw',
Expand Down
Loading

0 comments on commit 90aa927

Please sign in to comment.